Skip to content

Commit

Permalink
docs: add batch sample (#1559)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
JesseLovelace and gcf-owl-bot[bot] committed Aug 11, 2022
1 parent 840b08a commit 583bf73
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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) |
Expand Down
@@ -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<String, String> newMetadata = new HashMap<>();
newMetadata.put("keyToAddOrUpdate", "value");
Page<Blob> 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]
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> firstBlobMetadata = storage.get(BUCKET, "b/1.txt").getMetadata();
Map<String, String> secondBlobMetadata = storage.get(BUCKET, "b/2.txt").getMetadata();

assertEquals("value", firstBlobMetadata.get("keyToAddOrUpdate"));
assertEquals("value", secondBlobMetadata.get("keyToAddOrUpdate"));
}
}

0 comments on commit 583bf73

Please sign in to comment.