Nira
Nira

Reputation: 469

com.google.cloud.storage.StorageException 404 in blob.getContent()

I'm running a service that either creates or updates objects in a GCP bucket. I.e my code checks if the object exists, and if it does my code reads it, updates it and writes it back. Occasionally I'm getting an exception when trying to read the object. My code:

Storage storage = googleStorage.get();
BlobId blobId = BlobId.of(STORAGE_BUCKET, "path/to.obj"));
Blob blob = storage.get(blobId);

if (blob == null) return null;

byte[] blobContent = blob.getContent();
...

The stacktrace:

...
at com.google.cloud.storage.Blob.getContent(Blob.java:455)
at com.google.cloud.storage.StorageImpl.readAllBytes(StorageImpl.java:461)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at com.google.cloud.storage.StorageImpl$16.call(StorageImpl.java:461)
at com.google.cloud.storage.StorageImpl$16.call(StorageImpl.java:464)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.load(HttpStorageRpc.java:588)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:220)
No such object: bucket/path/to.obj
com.google.cloud.storage.StorageException: 404 Not Found

I would expect to get null in blob if the object does not exist, or to be able to read if blob isn't null.

This behavior results in the object being updated several times (not sure if this is because my code retries the call or because of something the storage library is doing).

I'm using google-cloud-storage 1.27.0, it happens about once per ~10K objects.

Upvotes: 4

Views: 3908

Answers (2)

Christopher P
Christopher P

Reputation: 1108

I’ve tested the code you provided and it appears to work in the desired way - the Blob object is assigned null if the Cloud Storage Object can’t be located or if the path is not correct.

The incidence of failure is quite insubstantial. Perhaps if you configure exponential backoff using the RetryParams class, then you can eliminate or reduce the impact of these failures.

Upvotes: 1

Raghuram Kasyap
Raghuram Kasyap

Reputation: 315

You don't need the BlobId. You can use this method:

Blob blob = storage.get(bucketName).get(pathToObject);

This method will return null if the blob does not exist at the specified path.

Upvotes: 0

Related Questions