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
fix: blob.reload() does not work as intuitively expected #308
Changes from 2 commits
9f187fb
11e9563
92fa2f4
bed0c4e
5fa111a
da4d96c
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 |
---|---|---|
|
@@ -525,24 +525,39 @@ public byte[] getContent(BlobSourceOption... options) { | |
} | ||
|
||
/** | ||
* Fetches current blob's latest information. Returns {@code null} if the blob does not exist. | ||
* Fetches the latest blob properties. Returns {@code null} if the blob no longer exists. | ||
* | ||
* <p>Example of getting the blob's latest information, if its generation does not match the | ||
* {@link Blob#getGeneration()} value, otherwise a {@link StorageException} is thrown. | ||
* <p>{@code options} parameter can contain the preconditions. For example, user may need to get | ||
* the blob properties only if the content has not been updated externally. {@code | ||
* StorageException} with the code {@code 412} is thrown if preconditions fail. | ||
* | ||
* <p>Example of retrieving the blob's latest information only if the content is not updated | ||
* externally: | ||
* | ||
* <pre>{@code | ||
* Blob latestBlob = blob.reload(BlobSourceOption.generationNotMatch()); | ||
* if (latestBlob == null) { | ||
* // the blob was not found | ||
* Blob blob = storage.get(BlobId.of(bucketName, blobName)); | ||
* | ||
* doSomething(); | ||
* | ||
* try { | ||
* blob = blob.reload(Blob.BlobSourceOption.generationMatch()); | ||
* } catch (StorageException e) { | ||
* if (e.getCode() == 412) { | ||
* // the content was updated externally | ||
* } else { | ||
* throw e; | ||
* } | ||
* } | ||
* }</pre> | ||
* | ||
* @param options blob read options | ||
* @return a {@code Blob} object with latest information or {@code null} if not found | ||
* @param options preconditions to fetch | ||
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. preconditions to use, see https://cloud.google.com/storage/docs/json_api/v1/objects/get for more information. 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. 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. Thanks @dmitry-fa, use:
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. done |
||
* @return a {@code Blob} object with latest information or {@code null} if no longer exists. | ||
* @throws StorageException upon failure | ||
*/ | ||
public Blob reload(BlobSourceOption... options) { | ||
return storage.get(getBlobId(), toGetOptions(this, options)); | ||
// BlobId with generation unset is needed to retrieve the latest version of the Blob | ||
BlobId idWithoutGeneration = BlobId.of(getBucket(), getName()); | ||
return storage.get(idWithoutGeneration, toGetOptions(this, options)); | ||
} | ||
|
||
/** | ||
|
@@ -732,6 +747,7 @@ public ReadChannel reader(BlobSourceOption... options) { | |
* // handle exception | ||
* } | ||
* } | ||
* blob = blob.reload(); | ||
* }</pre> | ||
* | ||
* @param options target blob options | ||
|
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.
user may need --> the user might want
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.
done