-
Notifications
You must be signed in to change notification settings - Fork 93
docs: Create Samples for transfer manager #2492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
393515f
docs: Create Samples for transfer manager
sydney-munro 4009db9
lint
sydney-munro 7e0264d
lint
sydney-munro 6f506b7
linter
sydney-munro a90c71c
fix test failing
sydney-munro b3ca39b
add more tests
sydney-munro d5ee8e9
linter
sydney-munro a7ac60a
move temp directory to rule
sydney-munro 2f4849b
fix failing test
sydney-munro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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()) | ||
|
sydney-munro marked this conversation as resolved.
|
||
| .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 hidden or 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 hidden or 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) | ||
|
BenWhitehead marked this conversation as resolved.
|
||
| 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 hidden or 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 hidden or 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(); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.