samples: Add samples for async download files #1470 (#1471) · googleapis/python-storage@8f56cc8 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 8f56cc8

Browse files
samples: Add samples for async download files #1470 (#1471)
* samples: Add samples for async download files #1470 * Add argument description for `async_download_blobs` function. Co-authored-by: cojenco <cathyo@google.com> * Addressed comments from cojenco@ * change download_as_string to bytes * Don't print blob contents after downloading * remove Google Inc , add Google LLC * pass list of file_names as one of the params. * fix lint issues * fix whitespace lints * remove unused variable i --------- Co-authored-by: cojenco <cathyo@google.com>
1 parent 63c1139 commit 8f56cc8

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

samples/snippets/snippets_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import storage_add_bucket_label
2828
import storage_async_upload
29+
import storage_async_download
2930
import storage_batch_request
3031
import storage_bucket_delete_default_kms_key
3132
import storage_change_default_storage_class
@@ -267,6 +268,19 @@ def test_async_upload(bucket, capsys):
267268
assert f"Uploaded 3 files to bucket {bucket.name}" in out
268269

269270

271+
def test_async_download(test_bucket, capsys):
272+
object_count = 3
273+
source_files = [f"async_sample_blob_{x}" for x in range(object_count)]
274+
for source in source_files:
275+
blob = test_bucket.blob(source)
276+
blob.upload_from_string(source)
277+
278+
asyncio.run(storage_async_download.async_download_blobs(test_bucket.name, *source_files))
279+
out, _ = capsys.readouterr()
280+
for x in range(object_count):
281+
assert f"Downloaded storage object async_sample_blob_{x}" in out
282+
283+
270284
def test_download_byte_range(test_blob):
271285
with tempfile.NamedTemporaryFile() as dest_file:
272286
storage_download_byte_range.download_byte_range(
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 asyncio
18+
import argparse
19+
20+
"""Sample that asynchronously downloads multiple files from GCS to application's memory.
21+
"""
22+
23+
24+
# [START storage_async_download]
25+
# This sample can be run by calling `async.run(async_download_blobs('bucket_name', ['file1', 'file2']))`
26+
async def async_download_blobs(bucket_name, *file_names):
27+
"""Downloads a number of files in parallel from the bucket.
28+
"""
29+
# The ID of your GCS bucket.
30+
# bucket_name = "your-bucket-name"
31+
32+
# The list of files names to download, these files should be present in bucket.
33+
# file_names = ["myfile1", "myfile2"]
34+
35+
import asyncio
36+
from google.cloud import storage
37+
38+
storage_client = storage.Client()
39+
bucket = storage_client.bucket(bucket_name)
40+
41+
loop = asyncio.get_running_loop()
42+
43+
tasks = []
44+
for file_name in file_names:
45+
blob = bucket.blob(file_name)
46+
# The first arg, None, tells it to use the default loops executor
47+
tasks.append(loop.run_in_executor(None, blob.download_as_bytes))
48+
49+
# If the method returns a value (such as download_as_bytes), gather will return the values
50+
_ = await asyncio.gather(*tasks)
51+
for file_name in file_names:
52+
print(f"Downloaded storage object {file_name}")
53+
54+
55+
# [END storage_async_download]
56+
57+
58+
if __name__ == "__main__":
59+
parser = argparse.ArgumentParser()
60+
parser.add_argument('-b', '--bucket_name', type=str, dest='bucket_name', help='provide the name of the GCS bucket')
61+
parser.add_argument(
62+
'-f', '--file_name',
63+
action='append',
64+
type=str,
65+
dest='file_names',
66+
help='Example: -f file1.txt or --file_name my_fav.mp4 . It can be used multiple times.'
67+
)
68+
args = parser.parse_args()
69+
70+
asyncio.run(async_download_blobs(args.bucket_name, *args.file_names))

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.