Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 126d94c

Browse files
authored
samples: Add snippets for upload/download from file-like object/stream (googleapis#709)
* samples: Add snippets for upload/download from file-like object/stream * change sample region tags to be uniform with other libraries * lint
1 parent fd5645b commit 126d94c

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

samples/snippets/snippets_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
import io
1617
import os
1718
import tempfile
1819
import time
@@ -44,6 +45,7 @@
4445
import storage_download_file
4546
import storage_download_into_memory
4647
import storage_download_public_file
48+
import storage_download_to_stream
4749
import storage_enable_bucket_lifecycle_management
4850
import storage_enable_versioning
4951
import storage_generate_signed_post_policy_v4
@@ -68,6 +70,7 @@
6870
import storage_set_metadata
6971
import storage_upload_file
7072
import storage_upload_from_memory
73+
import storage_upload_from_stream
7174
import storage_upload_with_kms_key
7275

7376
KMS_KEY = os.environ["CLOUD_KMS_KEY"]
@@ -204,6 +207,17 @@ def test_upload_blob_from_memory(test_bucket, capsys):
204207
assert "Hello, is it me you're looking for?" in out
205208

206209

210+
def test_upload_blob_from_stream(test_bucket, capsys):
211+
file_obj = io.StringIO()
212+
file_obj.write("This is test data.")
213+
storage_upload_from_stream.upload_blob_from_stream(
214+
test_bucket.name, file_obj, "test_upload_blob"
215+
)
216+
out, _ = capsys.readouterr()
217+
218+
assert "Stream data uploaded to {}".format("test_upload_blob") in out
219+
220+
207221
def test_upload_blob_with_kms(test_bucket):
208222
with tempfile.NamedTemporaryFile() as source_file:
209223
source_file.write(b"test")
@@ -247,6 +261,20 @@ def test_download_blob_into_memory(test_blob, capsys):
247261
assert "Hello, is it me you're looking for?" in out
248262

249263

264+
def test_download_blob_to_stream(test_blob, capsys):
265+
file_obj = io.BytesIO()
266+
storage_download_to_stream.download_blob_to_stream(
267+
test_blob.bucket.name, test_blob.name, file_obj
268+
)
269+
out, _ = capsys.readouterr()
270+
271+
file_obj.seek(0)
272+
content = file_obj.read()
273+
274+
assert "Downloaded blob" in out
275+
assert b"Hello, is it me you're looking for?" in content
276+
277+
250278
def test_blob_metadata(test_blob, capsys):
251279
storage_get_metadata.blob_metadata(test_blob.bucket.name, test_blob.name)
252280
out, _ = capsys.readouterr()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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+
# [START storage_stream_file_download]
18+
from google.cloud import storage
19+
20+
21+
def download_blob_to_stream(bucket_name, source_blob_name, file_obj):
22+
"""Downloads a blob to a stream or other file-like object."""
23+
24+
# The ID of your GCS bucket
25+
# bucket_name = "your-bucket-name"
26+
27+
# The ID of your GCS object (blob)
28+
# source_blob_name = "storage-object-name"
29+
30+
# The stream or file (file-like object) to which the blob will be written
31+
# import io
32+
# file_obj = io.BytesIO()
33+
34+
storage_client = storage.Client()
35+
36+
bucket = storage_client.bucket(bucket_name)
37+
38+
# Construct a client-side representation of a blob.
39+
# Note `Bucket.blob` differs from `Bucket.get_blob` in that it doesn't
40+
# retrieve metadata from Google Cloud Storage. As we don't use metadata in
41+
# this example, using `Bucket.blob` is preferred here.
42+
blob = bucket.blob(source_blob_name)
43+
blob.download_to_file(file_obj)
44+
45+
print("Downloaded blob {} to file-like object.".format(source_blob_name))
46+
47+
return file_obj
48+
# Before reading from file_obj, remember to rewind with file_obj.seek(0).
49+
50+
# [END storage_stream_file_download]

samples/snippets/storage_upload_from_memory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222

2323
def upload_blob_from_memory(bucket_name, contents, destination_blob_name):
2424
"""Uploads a file to the bucket."""
25+
2526
# The ID of your GCS bucket
2627
# bucket_name = "your-bucket-name"
28+
2729
# The contents to upload to the file
2830
# contents = "these are my contents"
31+
2932
# The ID of your GCS object
3033
# destination_blob_name = "storage-object-name"
3134

@@ -37,13 +40,13 @@ def upload_blob_from_memory(bucket_name, contents, destination_blob_name):
3740

3841
print(
3942
"{} with contents {} uploaded to {}.".format(
40-
destination_blob_name, contents, destination_blob_name
43+
destination_blob_name, contents, bucket_name
4144
)
4245
)
4346

44-
4547
# [END storage_file_upload_from_memory]
4648

49+
4750
if __name__ == "__main__":
4851
upload_blob_from_memory(
4952
bucket_name=sys.argv[1],
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 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+
# [START storage_stream_file_upload]
18+
from google.cloud import storage
19+
20+
21+
def upload_blob_from_stream(bucket_name, file_obj, destination_blob_name):
22+
"""Uploads bytes from a stream or other file-like object to a blob."""
23+
# The ID of your GCS bucket
24+
# bucket_name = "your-bucket-name"
25+
26+
# The stream or file (file-like object) from which to read
27+
# import io
28+
# file_obj = io.StringIO()
29+
# file_obj.write("This is test data.")
30+
31+
# The desired name of the uploaded GCS object (blob)
32+
# destination_blob_name = "storage-object-name"
33+
34+
# Construct a client-side representation of the blob.
35+
storage_client = storage.Client()
36+
bucket = storage_client.bucket(bucket_name)
37+
blob = bucket.blob(destination_blob_name)
38+
39+
# Rewind the stream to the beginning. This step can be omitted if the input
40+
# stream will always be at a correct position.
41+
file_obj.seek(0)
42+
43+
# Upload data from the stream to your bucket.
44+
blob.upload_from_file(file_obj)
45+
46+
print(
47+
"Stream data uploaded to {} in bucket {}.".format(
48+
destination_blob_name, bucket_name
49+
)
50+
)
51+
52+
# [END storage_stream_file_upload]

0 commit comments

Comments
 (0)