-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Create Samples for transfer manager (#2492)
* docs: Create Samples for transfer manager
- Loading branch information
1 parent
8b54549
commit e2030b2
Showing
5 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadBucket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.transfermanager; | ||
|
||
// [START storage_transfer_manager_download_bucket] | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.google.cloud.storage.transfermanager.DownloadResult; | ||
import com.google.cloud.storage.transfermanager.ParallelDownloadConfig; | ||
import com.google.cloud.storage.transfermanager.TransferManager; | ||
import com.google.cloud.storage.transfermanager.TransferManagerConfig; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
class DownloadBucket { | ||
|
||
public static void downloadBucketContents(String projectId, | ||
String bucketName, Path destinationDirectory) { | ||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
List<BlobInfo> blobs = storage | ||
.list(bucketName) | ||
.streamAll() | ||
.map(blob -> blob.asBlobInfo()) | ||
.collect(Collectors.toList()); | ||
TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); | ||
ParallelDownloadConfig parallelDownloadConfig = ParallelDownloadConfig.newBuilder() | ||
.setBucketName(bucketName) | ||
.setDownloadDirectory(destinationDirectory) | ||
.build(); | ||
|
||
List<DownloadResult> results = transferManager | ||
.downloadBlobs(blobs, parallelDownloadConfig) | ||
.getDownloadResults(); | ||
|
||
for (DownloadResult result : results) { | ||
System.out.println("Download of " + result.getInput().getName() | ||
+ " completed with status " | ||
+ result.getStatus()); | ||
} | ||
|
||
} | ||
|
||
} | ||
// [END storage_transfer_manager_download_bucket] |
51 changes: 51 additions & 0 deletions
51
samples/snippets/src/main/java/com/example/storage/transfermanager/DownloadMany.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.transfermanager; | ||
|
||
// [START storage_transfer_manager_download_many] | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.transfermanager.DownloadResult; | ||
import com.google.cloud.storage.transfermanager.ParallelDownloadConfig; | ||
import com.google.cloud.storage.transfermanager.TransferManager; | ||
import com.google.cloud.storage.transfermanager.TransferManagerConfig; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
class DownloadMany { | ||
|
||
public static void downloadManyBlobs(String bucketName, | ||
List<BlobInfo> blobs, Path destinationDirectory) { | ||
|
||
TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); | ||
ParallelDownloadConfig parallelDownloadConfig = ParallelDownloadConfig.newBuilder() | ||
.setBucketName(bucketName) | ||
.setDownloadDirectory(destinationDirectory) | ||
.build(); | ||
|
||
List<DownloadResult> results = transferManager | ||
.downloadBlobs(blobs, parallelDownloadConfig) | ||
.getDownloadResults(); | ||
|
||
for (DownloadResult result : results) { | ||
System.out.println("Download of " + result.getInput().getName() | ||
+ " completed with status " + result.getStatus()); | ||
} | ||
|
||
} | ||
|
||
} | ||
// [END storage_transfer_manager_download_many] |
59 changes: 59 additions & 0 deletions
59
samples/snippets/src/main/java/com/example/storage/transfermanager/UploadDirectory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.transfermanager; | ||
|
||
// [START storage_transfer_manager_upload_directory] | ||
import com.google.cloud.storage.transfermanager.ParallelUploadConfig; | ||
import com.google.cloud.storage.transfermanager.TransferManager; | ||
import com.google.cloud.storage.transfermanager.TransferManagerConfig; | ||
import com.google.cloud.storage.transfermanager.UploadResult; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
class UploadDirectory { | ||
|
||
public static void uploadDirectoryContents(String bucketName, Path sourceDirectory) | ||
throws IOException { | ||
TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); | ||
ParallelUploadConfig parallelUploadConfig = ParallelUploadConfig | ||
.newBuilder() | ||
.setBucketName(bucketName) | ||
.build(); | ||
|
||
// Create a list to store the file paths | ||
List<Path> filePaths = new ArrayList<>(); | ||
// Get all files in the directory | ||
// try-with-resource to ensure pathStream is closed | ||
try (Stream<Path> pathStream = Files.walk(sourceDirectory)) { | ||
pathStream.filter(Files::isRegularFile) | ||
.forEach(filePaths::add); | ||
} | ||
List<UploadResult> results = transferManager | ||
.uploadFiles(filePaths, parallelUploadConfig) | ||
.getUploadResults(); | ||
for (UploadResult result : results) { | ||
System.out.println("Upload for " + result.getInput().getName() | ||
+ " completed with status " + result.getStatus()); | ||
} | ||
} | ||
|
||
} | ||
// [END storage_transfer_manager_upload_directory] |
46 changes: 46 additions & 0 deletions
46
samples/snippets/src/main/java/com/example/storage/transfermanager/UploadMany.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.storage.transfermanager; | ||
|
||
// [START storage_transfer_manager_upload_many] | ||
import com.google.cloud.storage.transfermanager.ParallelUploadConfig; | ||
import com.google.cloud.storage.transfermanager.TransferManager; | ||
import com.google.cloud.storage.transfermanager.TransferManagerConfig; | ||
import com.google.cloud.storage.transfermanager.UploadResult; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
class UploadMany { | ||
|
||
public static void uploadManyFiles(String bucketName, List<Path> files) | ||
throws IOException { | ||
TransferManager transferManager = TransferManagerConfig.newBuilder().build().getService(); | ||
ParallelUploadConfig parallelUploadConfig = ParallelUploadConfig | ||
.newBuilder() | ||
.setBucketName(bucketName) | ||
.build(); | ||
List<UploadResult> results = transferManager | ||
.uploadFiles(files, parallelUploadConfig) | ||
.getUploadResults(); | ||
for (UploadResult result : results) { | ||
System.out.println("Upload for " + result.getInput().getName() | ||
+ " completed with status " + result.getStatus()); | ||
} | ||
} | ||
} | ||
// [END storage_transfer_manager_upload_many] |
119 changes: 119 additions & 0 deletions
119
.../snippets/src/test/java/com/example/storage/transfermanager/ITTransferManagerSamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.example.storage.transfermanager; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.BucketInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.testing.RemoteStorageHelper; | ||
import com.google.cloud.testing.junit4.StdOutCaptureRule; | ||
import com.google.common.collect.ImmutableList; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
|
||
public class ITTransferManagerSamples { | ||
private static final String BUCKET = RemoteStorageHelper.generateBucketName(); | ||
private static Storage storage; | ||
private static List<BlobInfo> blobs; | ||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
@Rule | ||
public final StdOutCaptureRule stdOutCaptureRule = new StdOutCaptureRule(); | ||
@Rule public final TemporaryFolder tmp = new TemporaryFolder(); | ||
@Rule public final TemporaryFolder tmpDirectory = new TemporaryFolder(); | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
RemoteStorageHelper helper = RemoteStorageHelper.create(); | ||
storage = helper.getOptions().getService(); | ||
storage.create(BucketInfo.of(BUCKET)); | ||
blobs = Arrays.asList(BlobInfo.newBuilder(BUCKET, "blob1").build(), | ||
BlobInfo.newBuilder(BUCKET, "blob2").build(), | ||
BlobInfo.newBuilder(BUCKET, "blob3").build()); | ||
for (BlobInfo blob : blobs) { | ||
storage.create(blob); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void uploadFiles() throws Exception { | ||
File tmpFile = File.createTempFile("file", ".txt"); | ||
File tmpFile2 = File.createTempFile("file2", ".txt"); | ||
File tmpFile3 = File.createTempFile("file3", ".txt"); | ||
List<Path> files = | ||
ImmutableList.of(tmpFile.toPath(), tmpFile2.toPath(), tmpFile3.toPath()); | ||
UploadMany.uploadManyFiles(BUCKET, files); | ||
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); | ||
assertThat(snippetOutput.contains("file")).isTrue(); | ||
assertThat(snippetOutput.contains("file2")).isTrue(); | ||
assertThat(snippetOutput.contains("file3")).isTrue(); | ||
} | ||
|
||
@Test | ||
public void uploadDirectory() throws IOException { | ||
File tmpFile = tmpDirectory.newFile("fileDirUpload.txt"); | ||
File tmpFile2 = tmpDirectory.newFile("fileDirUpload2.txt"); | ||
File tmpFile3 = tmpDirectory.newFile("fileDirUpload3.txt"); | ||
UploadDirectory.uploadDirectoryContents(BUCKET, tmpDirectory.getRoot().toPath()); | ||
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); | ||
assertThat(snippetOutput.contains("fileDirUpload.txt")).isTrue(); | ||
assertThat(snippetOutput.contains("fileDirUpload2.txt")).isTrue(); | ||
assertThat(snippetOutput.contains("fileDirUpload3.txt")).isTrue(); | ||
} | ||
|
||
@Test | ||
public void downloadBucket() { | ||
String downloadFullBucketName = RemoteStorageHelper.generateBucketName(); | ||
storage.create(BucketInfo.of(downloadFullBucketName)); | ||
List<BlobInfo> bucketBlobs = Arrays.asList( | ||
BlobInfo.newBuilder(downloadFullBucketName, "bucketb1").build(), | ||
BlobInfo.newBuilder(downloadFullBucketName, "bucketb2").build(), | ||
BlobInfo.newBuilder(downloadFullBucketName, "bucketb3").build()); | ||
for (BlobInfo blob : bucketBlobs) { | ||
storage.create(blob); | ||
} | ||
DownloadBucket | ||
.downloadBucketContents(PROJECT_ID, downloadFullBucketName, tmp.getRoot().toPath()); | ||
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); | ||
assertThat(snippetOutput.contains("bucketb1")).isTrue(); | ||
assertThat(snippetOutput.contains("bucketb2")).isTrue(); | ||
assertThat(snippetOutput.contains("bucketb3")).isTrue(); | ||
} | ||
|
||
@Test | ||
public void downloadFiles() { | ||
DownloadMany.downloadManyBlobs(BUCKET, blobs, tmp.getRoot().toPath()); | ||
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String(); | ||
assertThat(snippetOutput.contains("blob1")).isTrue(); | ||
assertThat(snippetOutput.contains("blob2")).isTrue(); | ||
assertThat(snippetOutput.contains("blob3")).isTrue(); | ||
} | ||
|
||
} |