This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
docs: Add snippets for upload_chunks_concurrently and add chunk_size #1135
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8421e5f
docs: Add snippets for upload_chunks_concurrently and add chunk_size
andrewsg 5a3874a
Merge branch 'main' into tm-mpu-sample
andrewsg 89dbd11
switch from 'processes' to 'workers' in sample nomenclature
andrewsg aa35cfd
copyright
andrewsg 18faaf1
tests
andrewsg 7cf3f00
Merge branch 'main' into tm-mpu-sample
andrewsg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 |
|---|---|---|
|
|
@@ -13,7 +13,9 @@ | |
| # limitations under the License. | ||
|
|
||
| # [START storage_transfer_manager_download_chunks_concurrently] | ||
| def download_chunks_concurrently(bucket_name, blob_name, filename, processes=8): | ||
| def download_chunks_concurrently( | ||
| bucket_name, blob_name, filename, chunk_size=32 * 1024 * 1024, workers=8 | ||
| ): | ||
| """Download a single file in chunks, concurrently in a process pool.""" | ||
|
|
||
| # The ID of your GCS bucket | ||
|
|
@@ -25,19 +27,29 @@ def download_chunks_concurrently(bucket_name, blob_name, filename, processes=8): | |
| # The destination filename or path | ||
| # filename = "" | ||
|
|
||
| # The size of each chunk. The performance impact of this value depends on | ||
| # the use case. The remote service has a minimum of 5 MiB and a maximum of | ||
| # 5 GiB. | ||
| # chunk_size = 32 * 1024 * 1024 (32 MiB) | ||
|
|
||
| # The maximum number of processes to use for the operation. The performance | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the rule of thumb here: "number of cores your CPU has"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, workloads with small files benefit from many times that number and workloads with large files max out the NIC below that, so the number of cores is not a good starting place.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something that would go into docs instead of sample comments? |
||
| # impact of this value depends on the use case, but smaller files usually | ||
| # benefit from a higher number of processes. Each additional process occupies | ||
| # some CPU and memory resources until finished. | ||
| # processes=8 | ||
| # some CPU and memory resources until finished. Threads can be used instead | ||
| # of processes by passing `worker_type=transfer_manager.THREAD`. | ||
| # workers=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) | ||
| transfer_manager.download_chunks_concurrently( | ||
| blob, filename, chunk_size=chunk_size, max_workers=workers | ||
| ) | ||
|
|
||
| print("Downloaded {} to {}.".format(blob_name, filename)) | ||
|
|
||
|
|
||
| # [END storage_transfer_manager_download_chunks_concurrently] | ||
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
samples/snippets/storage_transfer_manager_upload_chunks_concurrently.py
This file contains hidden or 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,57 @@ | ||
| # Copyright 2023 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. | ||
|
|
||
| # [START storage_transfer_manager_upload_chunks_concurrently] | ||
| def upload_chunks_concurrently( | ||
| bucket_name, | ||
| source_filename, | ||
| destination_blob_name, | ||
| chunk_size=32 * 1024 * 1024, | ||
| workers=8, | ||
| ): | ||
| """Upload a single file, in chunks, concurrently in a process pool.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-bucket-name" | ||
|
|
||
| # The path to your file to upload | ||
| # source_filename = "local/path/to/file" | ||
|
|
||
| # The ID of your GCS object | ||
| # destination_blob_name = "storage-object-name" | ||
|
|
||
| # The size of each chunk. The performance impact of this value depends on | ||
| # the use case. The remote service has a minimum of 5 MiB and a maximum of | ||
| # 5 GiB. | ||
| # chunk_size = 32 * 1024 * 1024 (32 MiB) | ||
|
|
||
| # The maximum number of processes to use for the operation. The performance | ||
| # impact of this value depends on the use case. Each additional process | ||
| # occupies some CPU and memory resources until finished. Threads can be used | ||
| # instead of processes by passing `worker_type=transfer_manager.THREAD`. | ||
| # workers=8 | ||
|
|
||
| from google.cloud.storage import Client, transfer_manager | ||
|
|
||
| storage_client = Client() | ||
| bucket = storage_client.bucket(bucket_name) | ||
| blob = bucket.blob(destination_blob_name) | ||
|
|
||
| transfer_manager.upload_chunks_concurrently( | ||
| source_filename, blob, chunk_size=chunk_size, max_workers=workers | ||
| ) | ||
|
|
||
| print(f"File {source_filename} uploaded to {destination_blob_name}.") | ||
|
|
||
|
|
||
| # [END storage_transfer_manager_upload_chunks_concurrently] |
This file contains hidden or 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 contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't exercise multiple chunks; recommend increasing the object size to test
chunk_sizeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sufficient to test the snippet. The feature itself is not under test here - it is fully covered in the integration tests, with appropriately-sized test files.