Skip to content

Commit b962d91

Browse files
committed
Add GetObjectChecksums.java sample
1 parent 9c4da91 commit b962d91

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.storage.object;
2+
3+
import com.google.cloud.storage.BlobId;
4+
import com.google.cloud.storage.BlobInfo;
5+
import com.google.cloud.storage.Storage;
6+
import com.google.cloud.storage.Storage.ChecksumRequestParams;
7+
import com.google.cloud.storage.StorageOptions;
8+
import java.io.IOException;
9+
10+
public class GetObjectChecksums {
11+
public static void getObjectChecksums(String bucketName, String objectName) throws IOException {
12+
// [START storage_get_object_checksums]
13+
// The ID of your GCS bucket
14+
// String bucketName = "your-bucket-name";
15+
// The ID of your GCS object
16+
// String objectName = "your-object-name";
17+
18+
Storage storage = StorageOptions.getDefaultInstance().getService();
19+
20+
ChecksumRequestParams checksumRequestParams =
21+
Storage.ChecksumRequestParams.newBuilder().setChecksumAlgorithm(Storage.ChecksumAlgorithm.CRC32C, Storage.ChecksumAlgorithm.MD5).build();
22+
23+
BlobInfo blobInfo =
24+
storage.get(BlobId.of(bucketName, objectName), Storage.BlobGetOption.checksumRequestParams(checksumRequestParams));
25+
26+
if (blobInfo == null) {
27+
System.out.println("\nThere is no such object!\n");
28+
return;
29+
}
30+
31+
System.out.println("Object: " + objectName + " in bucket: " + bucketName + " has CRC32C: " + blobInfo.getCrc32c() + " and MD5: " + blobInfo.getMd5());
32+
// [END storage_get_object_checksums]
33+
}
34+
}

0 commit comments

Comments
 (0)