Skip to content

Commit d1f6bcc

Browse files
authored
docs: Add in Transfer Manager chunked upload/download samples (#2518)
1 parent b7d673c commit d1f6bcc

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.example.storage.transfermanager;
19+
20+
// [START storage_transfer_manager_download_chunks_concurrently]
21+
import com.google.cloud.storage.BlobInfo;
22+
import com.google.cloud.storage.transfermanager.DownloadResult;
23+
import com.google.cloud.storage.transfermanager.ParallelDownloadConfig;
24+
import com.google.cloud.storage.transfermanager.TransferManager;
25+
import com.google.cloud.storage.transfermanager.TransferManagerConfig;
26+
import java.nio.file.Path;
27+
import java.util.List;
28+
29+
class AllowDivideAndConquerDownload {
30+
31+
public static void divideAndConquerDownloadAllowed(List<BlobInfo> blobs,
32+
String bucketName, Path destinationDirectory) {
33+
TransferManager transferManager = TransferManagerConfig.newBuilder()
34+
.setAllowDivideAndConquerDownload(true)
35+
.build()
36+
.getService();
37+
ParallelDownloadConfig parallelDownloadConfig = ParallelDownloadConfig.newBuilder()
38+
.setBucketName(bucketName)
39+
.setDownloadDirectory(destinationDirectory)
40+
.build();
41+
List<DownloadResult> results = transferManager
42+
.downloadBlobs(blobs, parallelDownloadConfig)
43+
.getDownloadResults();
44+
45+
for (DownloadResult result : results) {
46+
System.out.println("Download of " + result.getInput().getName()
47+
+ " completed with status "
48+
+ result.getStatus());
49+
}
50+
51+
}
52+
}
53+
// [END storage_transfer_manager_download_chunks_concurrently]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.example.storage.transfermanager;
19+
20+
// [START storage_transfer_manager_upload_chunks_concurrently]
21+
import com.google.cloud.storage.transfermanager.ParallelUploadConfig;
22+
import com.google.cloud.storage.transfermanager.TransferManager;
23+
import com.google.cloud.storage.transfermanager.TransferManagerConfig;
24+
import com.google.cloud.storage.transfermanager.UploadResult;
25+
import java.io.IOException;
26+
import java.nio.file.Path;
27+
import java.util.List;
28+
29+
class AllowParallelCompositeUpload {
30+
31+
public static void parallelCompositeUploadAllowed(String bucketName, List<Path> files)
32+
throws IOException {
33+
TransferManager transferManager = TransferManagerConfig
34+
.newBuilder()
35+
.setAllowParallelCompositeUpload(true)
36+
.build()
37+
.getService();
38+
ParallelUploadConfig parallelUploadConfig =
39+
ParallelUploadConfig.newBuilder().setBucketName(bucketName).build();
40+
List<UploadResult> results =
41+
transferManager.uploadFiles(files, parallelUploadConfig).getUploadResults();
42+
for (UploadResult result : results) {
43+
System.out.println(
44+
"Upload for "
45+
+ result.getInput().getName()
46+
+ " completed with status "
47+
+ result.getStatus());
48+
}
49+
}
50+
}
51+
// [END storage_transfer_manager_upload_chunks_concurrently]

samples/snippets/src/test/java/com/example/storage/transfermanager/ITTransferManagerSamples.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@
2525
import com.google.cloud.storage.testing.RemoteStorageHelper;
2626
import com.google.cloud.testing.junit4.StdOutCaptureRule;
2727
import com.google.common.collect.ImmutableList;
28+
import com.google.common.collect.ImmutableSet;
2829
import java.io.File;
2930
import java.io.IOException;
31+
import java.nio.ByteBuffer;
32+
import java.nio.channels.SeekableByteChannel;
33+
import java.nio.file.Files;
34+
import java.nio.file.OpenOption;
3035
import java.nio.file.Path;
36+
import java.nio.file.StandardOpenOption;
3137
import java.util.Arrays;
38+
import java.util.Collections;
3239
import java.util.List;
40+
import java.util.Set;
3341
import org.junit.BeforeClass;
3442
import org.junit.Rule;
3543
import org.junit.Test;
@@ -39,6 +47,7 @@ public class ITTransferManagerSamples {
3947
private static final String BUCKET = RemoteStorageHelper.generateBucketName();
4048
private static Storage storage;
4149
private static List<BlobInfo> blobs;
50+
private static List<BlobInfo> bigBlob;
4251
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4352
@Rule public final StdOutCaptureRule stdOutCaptureRule = new StdOutCaptureRule();
4453
@Rule public final TemporaryFolder tmp = new TemporaryFolder();
@@ -112,4 +121,23 @@ public void downloadFiles() {
112121
assertThat(snippetOutput.contains("blob2")).isTrue();
113122
assertThat(snippetOutput.contains("blob3")).isTrue();
114123
}
124+
125+
@Test
126+
public void uploadAllowPCU() throws IOException {
127+
File tmpFile = tmpDirectory.newFile("fileDirUpload.txt");
128+
AllowParallelCompositeUpload
129+
.parallelCompositeUploadAllowed(BUCKET, Collections.singletonList(tmpFile.toPath()));
130+
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String();
131+
assertThat(snippetOutput.contains("fileDirUpload.txt")).isTrue();
132+
}
133+
134+
@Test
135+
public void downloadAllowDivideAndConquer() {
136+
AllowDivideAndConquerDownload
137+
.divideAndConquerDownloadAllowed(blobs, BUCKET, tmp.getRoot().toPath());
138+
String snippetOutput = stdOutCaptureRule.getCapturedOutputAsUtf8String();
139+
assertThat(snippetOutput.contains("blob1")).isTrue();
140+
assertThat(snippetOutput.contains("blob2")).isTrue();
141+
assertThat(snippetOutput.contains("blob3")).isTrue();
142+
}
115143
}

0 commit comments

Comments
 (0)