From 583bf73f5d58aa5d79fbaa12b24407c558235eed Mon Sep 17 00:00:00 2001 From: JesseLovelace <43148100+JesseLovelace@users.noreply.github.com> Date: Thu, 11 Aug 2022 11:54:53 -0700 Subject: [PATCH] docs: add batch sample (#1559) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: add batch sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * copyright * fix whitespace Co-authored-by: Owl Bot --- README.md | 1 + .../object/BatchSetObjectMetadata.java | 67 +++++++++++++++++++ .../com/example/storage/ITObjectSnippets.java | 15 +++++ 3 files changed, 83 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/storage/object/BatchSetObjectMetadata.java diff --git a/README.md b/README.md index 05ffac31e..036b3d152 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,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 000000000..d7db73597 --- /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 3cb572dc9..5f1d1e939 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")); + } }