-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add sample and sample test for transfer manager (#1027)
* add sample and sample test for transfer manager download blob as chunks concurrently method * chore: modify format for int * chore: refactor transfer manager sample names and tests --------- Co-authored-by: Andrew Gorcester <[email protected]>
- Loading branch information
Showing
6 changed files
with
284 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
samples/snippets/storage_transfer_manager_download_all_blobs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 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. | ||
|
||
|
||
def download_all_blobs_with_transfer_manager( | ||
bucket_name, destination_directory="", threads=4 | ||
): | ||
"""Download all of the blobs in a bucket, concurrently in a thread pool. | ||
The filename of each blob once downloaded is derived from the blob name and | ||
the `destination_directory `parameter. For complete control of the filename | ||
of each blob, use transfer_manager.download_many() instead. | ||
Directories will be created automatically as needed, for instance to | ||
accommodate blob names that include slashes. | ||
""" | ||
|
||
# The ID of your GCS bucket | ||
# bucket_name = "your-bucket-name" | ||
|
||
# The directory on your computer to which to download all of the files. This | ||
# string is prepended (with os.path.join()) to the name of each blob to form | ||
# the full path. Relative paths and absolute paths are both accepted. An | ||
# empty string means "the current working directory". Note that this | ||
# parameter allows accepts directory traversal ("../" etc.) and is not | ||
# intended for unsanitized end user input. | ||
# destination_directory = "" | ||
|
||
# The number of threads to use for the operation. The performance impact of | ||
# this value depends on the use case, but generally, smaller files benefit | ||
# from more threads and larger files don't benefit from more threads. Too | ||
# many threads can slow operations, especially with large files, due to | ||
# contention over the Python GIL. | ||
# threads=4 | ||
|
||
from google.cloud.storage import Client, transfer_manager | ||
|
||
storage_client = Client() | ||
bucket = storage_client.bucket(bucket_name) | ||
|
||
blob_names = [blob.name for blob in bucket.list_blobs()] | ||
|
||
results = transfer_manager.download_many_to_path( | ||
bucket, blob_names, destination_directory=destination_directory, threads=threads | ||
) | ||
|
||
for name, result in zip(blob_names, results): | ||
# The results list is either `None` or an exception for each blob in | ||
# the input list, in order. | ||
|
||
if isinstance(result, Exception): | ||
print("Failed to download {} due to exception: {}".format(name, result)) | ||
else: | ||
print("Downloaded {} to {}.".format(name, destination_directory + name)) |
44 changes: 44 additions & 0 deletions
44
samples/snippets/storage_transfer_manager_download_chunks_concurrently.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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. | ||
|
||
|
||
def download_chunks_concurrently(bucket_name, blob_name, filename, processes=8): | ||
"""Download a single file in chunks, concurrently.""" | ||
|
||
# The ID of your GCS bucket | ||
# bucket_name = "your-bucket-name" | ||
|
||
# The file to be downloaded | ||
# blob_name = "target-file" | ||
|
||
# The destination filename or path | ||
# filename = "" | ||
|
||
# The maximum number of worker processes that should be used to handle the | ||
# workload of downloading the blob concurrently. PROCESS worker type uses more | ||
# system resources (both memory and CPU) and can result in faster operations | ||
# when working with large files. The optimal number of workers depends heavily | ||
# on the specific use case. Refer to the docstring of the underlining method | ||
# for more details. | ||
# processes=8 | ||
|
||
from google.cloud.storage import Client, transfer_manager | ||
|
||
storage_client = Client() | ||
bucket = storage_client.bucket(bucket_name) | ||
blob = bucket.blob(blob_name) | ||
|
||
transfer_manager.download_chunks_concurrently(blob, filename, max_workers=processes) | ||
|
||
print("Downloaded {} to {}.".format(blob_name, filename)) |
Oops, something went wrong.