19
19
// [START storage_copy_file]
20
20
21
21
import com .google .cloud .storage .BlobId ;
22
+ import com .google .cloud .storage .BlobInfo ;
23
+ import com .google .cloud .storage .CopyWriter ;
22
24
import com .google .cloud .storage .Storage ;
25
+ import com .google .cloud .storage .Storage .CopyRequest ;
23
26
import com .google .cloud .storage .StorageOptions ;
24
27
25
28
public class CopyObject {
26
29
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 {
28
32
// The ID of your GCP project
29
33
// String projectId = "your-project-id";
30
34
@@ -37,40 +41,46 @@ public static void copyObject(
37
41
// The ID of the bucket to copy the object to
38
42
// String targetBucketName = "target-object-bucket";
39
43
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 );
45
49
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
+ }
54
65
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 ( );
66
77
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
+ }
74
84
}
75
85
}
76
86
// [END storage_copy_file]
0 commit comments