diff --git a/README.md b/README.md index 4efd58f5d5..47903c10b2 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/ | Get Hmac Key | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/hmac/GetHmacKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/hmac/GetHmacKey.java) | | List Hmac Keys | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/hmac/ListHmacKeys.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/hmac/ListHmacKeys.java) | | Add File Owner | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/AddFileOwner.java) | +| Batch Set Object Metadata | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java) | | Change Object Csek To Kms | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/ChangeObjectCsekToKms.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/ChangeObjectCsekToKms.java) | | Change Object Storage Class | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/ChangeObjectStorageClass.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/ChangeObjectStorageClass.java) | | Compose Object | [source code](https://github.com/googleapis/java-storage/blob/main/samples/snippets/src/main/java/com/example/storage/object/ComposeObject.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-storage&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/storage/object/ComposeObject.java) | diff --git a/samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java b/samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java new file mode 100644 index 0000000000..d7db73597b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java @@ -0,0 +1,67 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.storage.object; + +// [START storage_batch_request] +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageBatch; +import com.google.cloud.storage.StorageOptions; +import java.util.HashMap; +import java.util.Map; + +public class BatchSetObjectMetadata { + public static void batchSetObjectMetadata( + String projectId, String bucketName, String directoryPrefix) { + // The ID of your GCP project + // String projectId = "your-project-id"; + + // The ID of your GCS bucket + // String bucketName = "your-unique-bucket-name"; + + // The directory prefix. All objects in the bucket with this prefix will have their metadata + // updated + // String directoryPrefix = "yourDirectory/"; + + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + Map newMetadata = new HashMap<>(); + newMetadata.put("keyToAddOrUpdate", "value"); + Page blobs = + storage.list( + bucketName, + Storage.BlobListOption.prefix(directoryPrefix), + Storage.BlobListOption.currentDirectory()); + StorageBatch batchRequest = storage.batch(); + + // Add all blobs with the given prefix to the batch request + for (Blob blob : blobs.iterateAll()) { + batchRequest.update(blob.toBuilder().setMetadata(newMetadata).build()); + } + + // Execute the batch request + batchRequest.submit(); + + System.out.println( + "All blobs in bucket " + + bucketName + + " with prefix '" + + directoryPrefix + + "' had their metadata updated."); + } +} +// [END storage_batch_request] diff --git a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java index 3cb572dc94..5f1d1e9394 100644 --- a/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java +++ b/samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import com.example.storage.object.BatchSetObjectMetadata; import com.example.storage.object.ChangeObjectCsekToKms; import com.example.storage.object.ChangeObjectStorageClass; import com.example.storage.object.ComposeObject; @@ -416,4 +417,18 @@ public void testUploadKMSEncryptedObject() { UploadKmsEncryptedObject.uploadKmsEncryptedObject(PROJECT_ID, BUCKET, blobName, KMS_KEY_NAME); assertNotNull(storage.get(BUCKET, blobName)); } + + @Test + public void testBatchSetObjectMetadata() { + storage.create(BlobInfo.newBuilder(BUCKET, "b/1.txt").build()); + storage.create(BlobInfo.newBuilder(BUCKET, "b/2.txt").build()); + + BatchSetObjectMetadata.batchSetObjectMetadata(PROJECT_ID, BUCKET, "b/"); + + Map firstBlobMetadata = storage.get(BUCKET, "b/1.txt").getMetadata(); + Map secondBlobMetadata = storage.get(BUCKET, "b/2.txt").getMetadata(); + + assertEquals("value", firstBlobMetadata.get("keyToAddOrUpdate")); + assertEquals("value", secondBlobMetadata.get("keyToAddOrUpdate")); + } }