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 1 commit
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,38 @@ 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. E.g. 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 id = getBlobId(); | ||
return storage.get(BlobId.of(id.getBucket(), id.getName()), toGetOptions(this, options)); | ||
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. Why do you make a copy of the ID here? 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. BlobId includes three components: bucket, name and generation. I form BlobId without generation to request the latest version of the blob. 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. OK, please add a comment explaining this 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 |
||
} | ||
|
||
/** | ||
|
@@ -732,6 +746,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.
E.g. --> For example,
Google style prohibits Latin abbreviations
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.
E.g. is widely used, perhaps we need to collect such things and do some code clean up.