Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 971ca5d

Browse files
authored
docs: update storage_copy_file to include MegabytesCopiedPerChunk (#2910)
1 parent 1fb80bb commit 971ca5d

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

samples/snippets/src/main/java/com/example/storage/object/CopyObject.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
// [START storage_copy_file]
2020

2121
import com.google.cloud.storage.BlobId;
22+
import com.google.cloud.storage.BlobInfo;
23+
import com.google.cloud.storage.CopyWriter;
2224
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.Storage.CopyRequest;
2326
import com.google.cloud.storage.StorageOptions;
2427

2528
public class CopyObject {
2629
public static void copyObject(
27-
String projectId, String sourceBucketName, String objectName, String targetBucketName) {
30+
String projectId, String sourceBucketName, String objectName, String targetBucketName)
31+
throws Exception {
2832
// The ID of your GCP project
2933
// String projectId = "your-project-id";
3034

@@ -37,40 +41,46 @@ public static void copyObject(
3741
// The ID of the bucket to copy the object to
3842
// String targetBucketName = "target-object-bucket";
3943

40-
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
41-
BlobId source = BlobId.of(sourceBucketName, objectName);
42-
BlobId target =
43-
BlobId.of(
44-
targetBucketName, objectName); // you could change "objectName" to rename the object
44+
try (Storage storage =
45+
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
46+
BlobId sourceId = BlobId.of(sourceBucketName, objectName);
47+
// you could change "objectName" to rename the object
48+
BlobId targetId = BlobId.of(targetBucketName, objectName);
4549

46-
// Optional: set a generation-match precondition to avoid potential race
47-
// conditions and data corruptions. The request returns a 412 error if the
48-
// preconditions are not met.
49-
Storage.BlobTargetOption precondition;
50-
if (storage.get(targetBucketName, objectName) == null) {
51-
// For a target object that does not yet exist, set the DoesNotExist precondition.
52-
// This will cause the request to fail if the object is created before the request runs.
53-
precondition = Storage.BlobTargetOption.doesNotExist();
50+
// Recommended: set a generation-match precondition to avoid potential race
51+
// conditions and data corruptions. The request returns a 412 error if the
52+
// preconditions are not met.
53+
Storage.BlobTargetOption precondition;
54+
BlobInfo existingTarget = storage.get(targetBucketName, objectName);
55+
if (existingTarget == null) {
56+
// For a target object that does not yet exist, set the DoesNotExist precondition.
57+
// This will cause the request to fail if the object is created before the request runs.
58+
precondition = Storage.BlobTargetOption.doesNotExist();
59+
} else {
60+
// If the destination already exists in your bucket, instead set a generation-match
61+
// precondition. This will cause the request to fail if the existing object's generation
62+
// changes before the request runs.
63+
precondition = Storage.BlobTargetOption.generationMatch(existingTarget.getGeneration());
64+
}
5465

55-
} else {
56-
// If the destination already exists in your bucket, instead set a generation-match
57-
// precondition. This will cause the request to fail if the existing object's generation
58-
// changes before the request runs.
59-
precondition =
60-
Storage.BlobTargetOption.generationMatch(
61-
storage.get(targetBucketName, objectName).getGeneration());
62-
}
63-
64-
storage.copy(
65-
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());
66+
CopyRequest copyRequest =
67+
CopyRequest.newBuilder()
68+
.setSource(sourceId)
69+
.setTarget(targetId, precondition)
70+
// limit the number of bytes Cloud Storage will attempt to copy before responding to
71+
// an individual request.
72+
// If you see Read Timeout errors, try reducing this value.
73+
.setMegabytesCopiedPerChunk(2048L) // 2GiB
74+
.build();
75+
CopyWriter copyWriter = storage.copy(copyRequest);
76+
BlobInfo successfulCopyResult = copyWriter.getResult();
6677

67-
System.out.println(
68-
"Copied object "
69-
+ objectName
70-
+ " from bucket "
71-
+ sourceBucketName
72-
+ " to "
73-
+ targetBucketName);
78+
System.out.printf(
79+
"Copied object gs://%s/%s to %s%n",
80+
sourceBucketName,
81+
objectName,
82+
successfulCopyResult.getBlobId().toGsUtilUriWithGeneration());
83+
}
7484
}
7585
}
7686
// [END storage_copy_file]

samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testChangeObjectStorageClass() {
134134
}
135135

136136
@Test
137-
public void testCopyObject() {
137+
public void testCopyObject() throws Exception {
138138
String newBucket = RemoteStorageHelper.generateBucketName();
139139
storage.create(BucketInfo.newBuilder(newBucket).build());
140140
try {

0 commit comments

Comments
 (0)