Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this class only contains static methods, it should be marked as final to prevent it from being subclassed. It's also a good practice to add a private constructor to prevent instantiation. You can add private GetObjectChecksums() {} inside the class.

Suggested change
public class GetObjectChecksums {
public final class GetObjectChecksums {

public static void getObjectChecksums(String bucketName, String objectName) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The throws IOException clause is unnecessary. The storage.get() method throws StorageException, which is a RuntimeException and does not need to be declared in the method signature. Removing it makes the signature more accurate.

Suggested change
public static void getObjectChecksums(String bucketName, String objectName) throws IOException {
public static void getObjectChecksums(String bucketName, String objectName) {

// [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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The System.out.println() method already adds a newline character at the end of the output, so the trailing \n is redundant.

Suggested change
System.out.println("\nThere is no such object!\n");
System.out.println("\nThere is no such object!");

return;
}

System.out.println("Object: " + objectName + " in bucket: " + bucketName + " has CRC32C: " + blobInfo.getCrc32c() + " and MD5: " + blobInfo.getMd5());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using System.out.printf() can make this line more readable and is generally preferred over string concatenation, especially with multiple variables.

Suggested change
System.out.println("Object: " + objectName + " in bucket: " + bucketName + " has CRC32C: " + blobInfo.getCrc32c() + " and MD5: " + blobInfo.getMd5());
System.out.printf("Object: %s in bucket: %s has CRC32C: %s and MD5: %s%n", objectName, bucketName, blobInfo.getCrc32c(), blobInfo.getMd5());

// [END storage_get_object_checksums]
}
}