Skip to content

Commit 82206f4

Browse files
authored
samples: storage_download_byte_range (googleapis#674)
* samples: storage_download_byte_range * added spacing * updated readme * fixed test
1 parent 0e1a0ff commit 82206f4

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

samples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ for more detailed instructions.
7676
* [Disable Requester Pays](#disable-requester-pays)
7777
* [Disable Uniform Bucket Level Access](#disable-uniform-bucket-level-access)
7878
* [Disable Versioning](#disable-versioning)
79+
* [Download Byte Range](#download-byte-range)
7980
* [Download Encrypted File](#download-encrypted-file)
8081
* [Download File](#download-file)
8182
* [Download File Requester Pays](#download-file-requester-pays)
@@ -427,6 +428,15 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa
427428
428429
`python storage_disable_versioning.py <BUCKET_NAME>`
429430
431+
-----
432+
### Download Byte Range
433+
[![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_download_byte_range.py,samples/README.md)
434+
435+
View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_download_byte_range.py). To run this sample:
436+
437+
438+
`python storage_download_byte_range.py <BUCKET_NAME> <SOURCE_BLOB_NAME> <START_BYTE> <END_BYTE> <DESTINATION_FILE_NAME> <>BASE64_ENCRYPTION_KEY`
439+
430440
-----
431441
### Download Encrypted File
432442
[![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_download_encrypted_file.py,samples/README.md)

samples/snippets/snippets_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import storage_delete_file_archived_generation
3838
import storage_disable_bucket_lifecycle_management
3939
import storage_disable_versioning
40+
import storage_download_byte_range
4041
import storage_download_file
4142
import storage_download_into_memory
4243
import storage_download_public_file
@@ -211,6 +212,14 @@ def test_upload_blob_with_kms(test_bucket):
211212
assert kms_blob.kms_key_name.startswith(KMS_KEY)
212213

213214

215+
def test_download_byte_range(test_blob):
216+
with tempfile.NamedTemporaryFile() as dest_file:
217+
storage_download_byte_range.download_byte_range(
218+
test_blob.bucket.name, test_blob.name, 0, 4, dest_file.name
219+
)
220+
assert dest_file.read() == b'Hello'
221+
222+
214223
def test_download_blob(test_blob):
215224
with tempfile.NamedTemporaryFile() as dest_file:
216225
storage_download_file.download_blob(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
# [START storage_download_byte_range]
20+
from google.cloud import storage
21+
22+
23+
def download_byte_range(
24+
bucket_name, source_blob_name, start_byte, end_byte, destination_file_name
25+
):
26+
"""Downloads a blob from the bucket."""
27+
# The ID of your GCS bucket
28+
# bucket_name = "your-bucket-name"
29+
30+
# The ID of your GCS object
31+
# source_blob_name = "storage-object-name"
32+
33+
# The starting byte at which to begin the download
34+
# start_byte = 0
35+
36+
# The ending byte at which to end the download
37+
# end_byte = 20
38+
39+
# The path to which the file should be downloaded
40+
# destination_file_name = "local/path/to/file"
41+
42+
storage_client = storage.Client()
43+
44+
bucket = storage_client.bucket(bucket_name)
45+
46+
# Construct a client side representation of a blob.
47+
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
48+
# any content from Google Cloud Storage. As we don't need additional data,
49+
# using `Bucket.blob` is preferred here.
50+
blob = bucket.blob(source_blob_name)
51+
blob.download_to_filename(destination_file_name, start=start_byte, end=end_byte)
52+
53+
print(
54+
"Downloaded bytes {} to {} of object {} from bucket {} to local file {}.".format(
55+
start_byte, end_byte, source_blob_name, bucket_name, destination_file_name
56+
)
57+
)
58+
59+
60+
# [END storage_download_byte_range]
61+
62+
if __name__ == "__main__":
63+
download_byte_range(
64+
bucket_name=sys.argv[1],
65+
source_blob_name=sys.argv[2],
66+
start_byte=sys.argv[3],
67+
end_byte=sys.argv[4],
68+
destination_file_name=sys.argv[5],
69+
)

0 commit comments

Comments
 (0)