-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add GetObjectChecksums.java sample #10221
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| package com.example.storage.object; | ||||||
|
|
||||||
| import com.google.cloud.storage.BlobId; | ||||||
| import com.google.cloud.storage.BlobInfo; | ||||||
| import com.google.cloud.storage.Storage; | ||||||
| import com.google.cloud.storage.Storage.ChecksumRequestParams; | ||||||
| import com.google.cloud.storage.StorageOptions; | ||||||
| import java.io.IOException; | ||||||
|
|
||||||
| public class GetObjectChecksums { | ||||||
| public static void getObjectChecksums(String bucketName, String objectName) throws IOException { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| // [START storage_get_object_checksums] | ||||||
| // The ID of your GCS bucket | ||||||
| // String bucketName = "your-bucket-name"; | ||||||
| // The ID of your GCS object | ||||||
| // String objectName = "your-object-name"; | ||||||
|
|
||||||
| Storage storage = StorageOptions.getDefaultInstance().getService(); | ||||||
|
|
||||||
| ChecksumRequestParams checksumRequestParams = | ||||||
| Storage.ChecksumRequestParams.newBuilder().setChecksumAlgorithm(Storage.ChecksumAlgorithm.CRC32C, Storage.ChecksumAlgorithm.MD5).build(); | ||||||
|
|
||||||
| BlobInfo blobInfo = | ||||||
| storage.get(BlobId.of(bucketName, objectName), Storage.BlobGetOption.checksumRequestParams(checksumRequestParams)); | ||||||
|
|
||||||
| if (blobInfo == null) { | ||||||
| System.out.println("\nThere is no such object!\n"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| System.out.println("Object: " + objectName + " in bucket: " + bucketName + " has CRC32C: " + blobInfo.getCrc32c() + " and MD5: " + blobInfo.getMd5()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||
| // [END storage_get_object_checksums] | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this class only contains static methods, it should be marked as
finalto prevent it from being subclassed. It's also a good practice to add a private constructor to prevent instantiation. You can addprivate GetObjectChecksums() {}inside the class.