Content-Length: 467477 | pFad | https://togithub.com/googleapis/java-storage/commit/583bf73f5d58aa5d79fbaa12b24407c558235eed

E47 docs: add batch sample (#1559) · googleapis/java-storage@583bf73 · GitHub
Skip to content

Commit 583bf73

Browse files
docs: add batch sample (#1559)
* 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>
1 parent 840b08a commit 583bf73

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-storage/tree/
285285
| 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) |
286286
| 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) |
287287
| 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) |
288+
| 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) |
288289
| 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) |
289290
| 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) |
290291
| 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) |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.object;
18+
19+
// [START storage_batch_request]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.storage.Blob;
22+
import com.google.cloud.storage.Storage;
23+
import com.google.cloud.storage.StorageBatch;
24+
import com.google.cloud.storage.StorageOptions;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
public class BatchSetObjectMetadata {
29+
public static void batchSetObjectMetadata(
30+
String projectId, String bucketName, String directoryPrefix) {
31+
// The ID of your GCP project
32+
// String projectId = "your-project-id";
33+
34+
// The ID of your GCS bucket
35+
// String bucketName = "your-unique-bucket-name";
36+
37+
// The directory prefix. All objects in the bucket with this prefix will have their metadata
38+
// updated
39+
// String directoryPrefix = "yourDirectory/";
40+
41+
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
42+
Map<String, String> newMetadata = new HashMap<>();
43+
newMetadata.put("keyToAddOrUpdate", "value");
44+
Page<Blob> blobs =
45+
storage.list(
46+
bucketName,
47+
Storage.BlobListOption.prefix(directoryPrefix),
48+
Storage.BlobListOption.currentDirectory());
49+
StorageBatch batchRequest = storage.batch();
50+
51+
// Add all blobs with the given prefix to the batch request
52+
for (Blob blob : blobs.iterateAll()) {
53+
batchRequest.update(blob.toBuilder().setMetadata(newMetadata).build());
54+
}
55+
56+
// Execute the batch request
57+
batchRequest.submit();
58+
59+
System.out.println(
60+
"All blobs in bucket "
61+
+ bucketName
62+
+ " with prefix '"
63+
+ directoryPrefix
64+
+ "' had their metadata updated.");
65+
}
66+
}
67+
// [END storage_batch_request]

samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertNull;
2525
import static org.junit.Assert.assertTrue;
2626

27+
import com.example.storage.object.BatchSetObjectMetadata;
2728
import com.example.storage.object.ChangeObjectCsekToKms;
2829
import com.example.storage.object.ChangeObjectStorageClass;
2930
import com.example.storage.object.ComposeObject;
@@ -416,4 +417,18 @@ public void testUploadKMSEncryptedObject() {
416417
UploadKmsEncryptedObject.uploadKmsEncryptedObject(PROJECT_ID, BUCKET, blobName, KMS_KEY_NAME);
417418
assertNotNull(storage.get(BUCKET, blobName));
418419
}
420+
421+
@Test
422+
public void testBatchSetObjectMetadata() {
423+
storage.create(BlobInfo.newBuilder(BUCKET, "b/1.txt").build());
424+
storage.create(BlobInfo.newBuilder(BUCKET, "b/2.txt").build());
425+
426+
BatchSetObjectMetadata.batchSetObjectMetadata(PROJECT_ID, BUCKET, "b/");
427+
428+
Map<String, String> firstBlobMetadata = storage.get(BUCKET, "b/1.txt").getMetadata();
429+
Map<String, String> secondBlobMetadata = storage.get(BUCKET, "b/2.txt").getMetadata();
430+
431+
assertEquals("value", firstBlobMetadata.get("keyToAddOrUpdate"));
432+
assertEquals("value", secondBlobMetadata.get("keyToAddOrUpdate"));
433+
}
419434
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://togithub.com/googleapis/java-storage/commit/583bf73f5d58aa5d79fbaa12b24407c558235eed

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy