Skip to content

Commit 30efbdb

Browse files
authored
samples: add batch request sample and test (#656)
* samples: add batch request sample and test * samples: update readme with new sample * add clarifying comment
1 parent 5e49e54 commit 30efbdb

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

samples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ for more detailed instructions.
4646
<summary><b>List of Samples</b></summary>
4747
4848
* [Activate HMAC Key](#activate-hmac-key)
49+
* [Batch Request](#batch-request)
4950
* [Add Bucket Conditional IAM Binding](#add-bucket-conditional-iam-binding)
5051
* [Add Bucket Default Owner](#add-bucket-default-owner)
5152
* [Add Bucket IAM Member](#add-bucket-iam-member)
@@ -157,6 +158,15 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa
157158
158159
`python storage_activate_hmac_key.py <ACCESS_ID> <PROJECT_ID>`
159160
161+
-----
162+
### Batch Request
163+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/snippets/storage_batch_request.py,samples/README.md)
164+
165+
View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_batch_request.py). To run this sample:
166+
167+
168+
`python storage_batch_request.py <BUCKET_NAME> <PREFIX>`
169+
160170
-----
161171
162172
### Add Bucket Conditional IAM Binding

samples/snippets/snippets_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import requests
2424

2525
import storage_add_bucket_label
26+
import storage_batch_request
2627
import storage_bucket_delete_default_kms_key
2728
import storage_change_default_storage_class
2829
import storage_change_file_storage_class
@@ -538,3 +539,17 @@ def test_storage_configure_retries(test_blob, capsys):
538539
assert "The following library method is customized to be retried" in out
539540
assert "_should_retry" in out
540541
assert "initial=1.5, maximum=45.0, multiplier=1.2, deadline=500.0" in out
542+
543+
544+
def test_batch_request(test_bucket):
545+
blob1 = test_bucket.blob("b/1.txt")
546+
blob2 = test_bucket.blob("b/2.txt")
547+
blob1.upload_from_string("hello world")
548+
blob2.upload_from_string("hello world")
549+
550+
storage_batch_request.batch_request(test_bucket.name, "b/")
551+
blob1.reload()
552+
blob2.reload()
553+
554+
assert blob1.metadata.get("your-metadata-key") == "your-metadata-value"
555+
assert blob2.metadata.get("your-metadata-key") == "your-metadata-value"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the 'License');
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sys
18+
19+
"""Sample that uses a batch request.
20+
This sample is used on this page:
21+
https://cloud.google.com/storage/docs/batch
22+
For more information, see README.md.
23+
"""
24+
25+
# [START storage_batch_request]
26+
27+
from google.cloud import storage
28+
29+
30+
def batch_request(bucket_name, prefix=None):
31+
"""Use a batch request to patch a list of objects with the given prefix in a bucket."""
32+
# The ID of your GCS bucket
33+
# bucket_name = "my-bucket"
34+
# The prefix of the object paths
35+
# prefix = "directory-prefix/"
36+
37+
client = storage.Client()
38+
bucket = client.bucket(bucket_name)
39+
40+
# Accumulate in a list the objects with a given prefix.
41+
blobs_to_patch = [blob for blob in bucket.list_blobs(prefix=prefix)]
42+
43+
# Use a batch context manager to edit metadata in the list of blobs.
44+
# The batch request is sent out when the context manager closes.
45+
# No more than 100 calls should be included in a single batch request.
46+
with client.batch():
47+
for blob in blobs_to_patch:
48+
metadata = {"your-metadata-key": "your-metadata-value"}
49+
blob.metadata = metadata
50+
blob.patch()
51+
52+
print(
53+
f"Batch request edited metadata for all objects with the given prefix in {bucket.name}."
54+
)
55+
56+
57+
# [END storage_batch_request]
58+
59+
if __name__ == "__main__":
60+
batch_request(bucket_name=sys.argv[1], prefix=sys.argv[2])

0 commit comments

Comments
 (0)