@@ -1989,60 +1989,73 @@ Blob create(
19891989 Bucket update (BucketInfo bucketInfo , BucketTargetOption ... options );
19901990
19911991 /**
1992- * Updates blob information. Original metadata are merged with metadata in the provided {@code
1993- * blobInfo}. To replace metadata instead you first have to unset them. Unsetting metadata can be
1994- * done by setting the provided {@code blobInfo}'s metadata to {@code null}. Accepts an optional
1995- * userProject {@link BlobTargetOption} option which defines the project id to assign operational
1996- * costs.
1992+ * Updates the blob properties if the preconditions specified by {@code options} are met. The
1993+ * property update works as described in {@link #update(BlobInfo)}.
19971994 *
1998- * <p>Example of udating a blob, only if the blob's metageneration matches a value, otherwise a
1999- * {@link StorageException} is thrown.
1995+ * <p>{@code options} parameter can contain the preconditions for applying the update. E.g. update
1996+ * of the blob properties might be required only if the properties have not been updated
1997+ * externally. {@code StorageException} with the code {@code 412} is thrown if preconditions fail.
1998+ *
1999+ * <p>Example of updating the content type only if the properties are not updated externally:
20002000 *
20012001 * <pre>{@code
2002- * String bucketName = "my-unique-bucket";
2003- * String blobName = "my-blob-name";
2004- * Blob blob = storage.get(bucketName, blobName);
2005- * BlobInfo updatedInfo = blob.toBuilder().setContentType("text/plain").build();
2006- * storage.update(updatedInfo, BlobTargetOption.metagenerationMatch());
2002+ * BlobId blobId = BlobId.of(bucketName, blobName);
2003+ * BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
2004+ * Blob blob = storage.create(blobInfo);
2005+ *
2006+ * doSomething();
2007+ *
2008+ * BlobInfo update = blob.toBuilder().setContentType("multipart/form-data").build();
2009+ * Storage.BlobTargetOption option = Storage.BlobTargetOption.metagenerationMatch();
2010+ * try {
2011+ * storage.update(update, option);
2012+ * } catch (StorageException e) {
2013+ * if (e.getCode() == 412) {
2014+ * // the properties were updated externally
2015+ * } else {
2016+ * throw e;
2017+ * }
2018+ * }
20072019 * }</pre>
20082020 *
2021+ * @param blobInfo information to update
2022+ * @param options preconditions to apply the update
20092023 * @return the updated blob
20102024 * @throws StorageException upon failure
2025+ * @see <a
2026+ * href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">https://cloud.google.com/storage/docs/json_api/v1/objects/update</a>
20112027 */
20122028 Blob update (BlobInfo blobInfo , BlobTargetOption ... options );
20132029
20142030 /**
2015- * Updates blob information. Original metadata are merged with metadata in the provided {@code
2016- * blobInfo}. If the original metadata already contains a key specified in the provided {@code
2017- * blobInfo's} metadata map, it will be replaced by the new value. Removing metadata can be done
2018- * by setting that metadata's value to {@code null}.
2031+ * Updates the properties of the blob. This method issues an RPC request to merge the current blob
2032+ * properties with the properties in the provided {@code blobInfo}. Properties not defined in
2033+ * {@code blobInfo} will not be updated. To unset a blob property this property in {@code
2034+ * blobInfo} should be explicitly set to {@code null}.
20192035 *
2020- * <p>Example of adding new metadata values or updating existing ones.
2036+ * <p>Bucket or blob's name cannot be changed by this method. If you want to rename the blob or
2037+ * move it to a different bucket use the {@link Blob#copyTo} and {@link #delete} operations.
20212038 *
2022- * <pre>{@code
2023- * String bucketName = "my-unique-bucket";
2024- * String blobName = "my-blob-name";
2025- * Map<String, String> newMetadata = new HashMap<>();
2026- * newMetadata.put("keyToAddOrUpdate", "value");
2027- * Blob blob = storage.update(BlobInfo.newBuilder(bucketName, blobName)
2028- * .setMetadata(newMetadata)
2029- * .build());
2030- * }</pre>
2039+ * <p>Property update alters the blob metadata generation and doesn't alter the blob generation.
20312040 *
2032- * <p>Example of removing metadata values.
2041+ * <p>Example of how to update blob's user provided metadata and unset the content type:
20332042 *
20342043 * <pre>{@code
2035- * String bucketName = "my-unique-bucket";
2036- * String blobName = "my-blob-name";
2037- * Map<String, String> newMetadata = new HashMap<>();
2038- * newMetadata.put("keyToRemove", null);
2039- * Blob blob = storage.update(BlobInfo.newBuilder(bucketName, blobName)
2040- * .setMetadata(newMetadata)
2041- * .build());
2044+ * Map<String, String> metadataUpdate = new HashMap<>();
2045+ * metadataUpdate.put("keyToAdd", "new value");
2046+ * metadataUpdate.put("keyToRemove", null);
2047+ * BlobInfo blobUpdate = BlobInfo.newBuilder(bucketName, blobName)
2048+ * .setMetadata(metadataUpdate)
2049+ * .setContentType(null)
2050+ * .build();
2051+ * Blob blob = storage.update(blobUpdate);
20422052 * }</pre>
20432053 *
2054+ * @param blobInfo information to update
20442055 * @return the updated blob
20452056 * @throws StorageException upon failure
2057+ * @see <a
2058+ * href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">https://cloud.google.com/storage/docs/json_api/v1/objects/update</a>
20462059 */
20472060 Blob update (BlobInfo blobInfo );
20482061
@@ -2578,10 +2591,10 @@ Blob create(
25782591 List <Blob > get (Iterable <BlobId > blobIds );
25792592
25802593 /**
2581- * Updates the requested blobs. A batch request is used to perform this call. Original metadata
2582- * are merged with metadata in the provided {@code BlobInfo} objects. To replace metadata instead
2583- * you first have to unset them. Unsetting metadata can be done by setting the provided {@code
2584- * BlobInfo} objects metadata to {@code null}. See {@link #update(BlobInfo)} for a code example.
2594+ * Updates the requested blobs. A batch request is used to perform this call. The original
2595+ * properties are merged with the properties in the provided {@code BlobInfo} objects. Unsetting a
2596+ * property can be done by setting the property of the provided {@code BlobInfo} objects to {@code
2597+ * null}. See {@link #update(BlobInfo)} for a code example.
25852598 *
25862599 * <p>Example of updating information on several blobs using a single batch request.
25872600 *
@@ -2604,10 +2617,10 @@ Blob create(
26042617 List <Blob > update (BlobInfo ... blobInfos );
26052618
26062619 /**
2607- * Updates the requested blobs. A batch request is used to perform this call. Original metadata
2608- * are merged with metadata in the provided {@code BlobInfo} objects. To replace metadata instead
2609- * you first have to unset them. Unsetting metadata can be done by setting the provided {@code
2610- * BlobInfo} objects metadata to {@code null}. See {@link #update(BlobInfo)} for a code example.
2620+ * Updates the requested blobs. A batch request is used to perform this call. The original
2621+ * properties are merged with the properties in the provided {@code BlobInfo} objects. Unsetting a
2622+ * property can be done by setting the property of the provided {@code BlobInfo} objects to {@code
2623+ * null}. See {@link #update(BlobInfo)} for a code example.
26112624 *
26122625 * <p>Example of updating information on several blobs using a single batch request.
26132626 *
0 commit comments