Skip to content

Commit

Permalink
samples: added upload from/download into memory samples (#664)
Browse files Browse the repository at this point in the history
* samples: added upload from/download into memory samples

* linted files:

* responded to PR comments

* renamed blob variable

* responded to comments and updated readme

* updated copyright

* fixed test
  • Loading branch information
shaffeeullah authored Dec 10, 2021
1 parent 0459cb4 commit b841482
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ for instructions on setting up credentials for applications.
* [Download Encrypted File](#download-encrypted-file)
* [Download File](#download-file)
* [Download File Requester Pays](#download-file-requester-pays)
* [Download Into Memory](#download-into-memory)
* [Download Public File](#download-public-file)
* [Enable Bucket Lifecycle Management](#enable-bucket-lifecycle-management)
* [Enable Default Event Based Hold](#enable-default-event-based-hold)
Expand Down Expand Up @@ -144,6 +145,7 @@ for instructions on setting up credentials for applications.
* [Set Temporary Hold](#set-temporary-hold)
* [Upload Encrypted File](#upload-encrypted-file)
* [Upload File](#upload-file)
* [Upload From Memory](#upload-from-memory)
* [Upload With KMS Key](#upload-with-kms-key)
* [View Bucket IAM Members](#view-bucket-iam-members)

Expand Down Expand Up @@ -456,6 +458,15 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa

`python storage_download_file_requester_pays.py <BUCKET_NAME> <PROJECT_ID> <SOURCE_BLOB_NAME> <DESTINATION_FILE_NAME>`

-----
### Download Into Memory
[![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_into_memory.py,samples/README.md)

View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_download_into_memory.py). To run this sample:


`python storage_download_into_memory.py <BUCKET_NAME> <BLOB_NAME>`

-----
### Download Public File
[![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_public_file.py,samples/README.md)
Expand Down Expand Up @@ -1014,6 +1025,15 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa

`python storage_upload_file.py <BUCKET_NAME> <SOURCE_FILE_NAME> <DESTINATION_BLOB_NAME>`

-----
### Upload From Memory
[![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_upload_from_memory.py,samples/README.md)

View the [source code](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_upload_from_memory.py). To run this sample:


`python storage_upload_from_memory.py <BUCKET_NAME> <CONTENTS> <DESTINATION_BLOB_NAME>`

-----
### Upload With KMS Key
[![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_upload_with_kms_key.py,samples/README.md)
Expand Down
20 changes: 20 additions & 0 deletions samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import storage_disable_bucket_lifecycle_management
import storage_disable_versioning
import storage_download_file
import storage_download_into_memory
import storage_download_public_file
import storage_enable_bucket_lifecycle_management
import storage_enable_versioning
Expand All @@ -62,6 +63,7 @@
import storage_set_bucket_default_kms_key
import storage_set_metadata
import storage_upload_file
import storage_upload_from_memory
import storage_upload_with_kms_key

KMS_KEY = os.environ["CLOUD_KMS_KEY"]
Expand Down Expand Up @@ -189,6 +191,15 @@ def test_upload_blob(test_bucket):
)


def test_upload_blob_from_memory(test_bucket, capsys):
storage_upload_from_memory.upload_blob_from_memory(
test_bucket.name, "Hello, is it me you're looking for?", "test_upload_blob"
)
out, _ = capsys.readouterr()

assert "Hello, is it me you're looking for?" in out


def test_upload_blob_with_kms(test_bucket):
with tempfile.NamedTemporaryFile() as source_file:
source_file.write(b"test")
Expand All @@ -209,6 +220,15 @@ def test_download_blob(test_blob):
assert dest_file.read()


def test_download_blob_into_memory(test_blob, capsys):
storage_download_into_memory.download_blob_into_memory(
test_blob.bucket.name, test_blob.name
)
out, _ = capsys.readouterr()

assert "Hello, is it me you're looking for?" in out


def test_blob_metadata(test_blob, capsys):
storage_get_metadata.blob_metadata(test_blob.bucket.name, test_blob.name)
out, _ = capsys.readouterr()
Expand Down
55 changes: 55 additions & 0 deletions samples/snippets/storage_download_into_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python

# Copyright 2021 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.

import sys

# [START storage_download_file]
from google.cloud import storage


def download_blob_into_memory(bucket_name, blob_name):
"""Downloads a blob into memory."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"

# The ID of your GCS object
# blob_name = "storage-object-name"

storage_client = storage.Client()

bucket = storage_client.bucket(bucket_name)

# Construct a client side representation of a blob.
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
# any content from Google Cloud Storage. As we don't need additional data,
# using `Bucket.blob` is preferred here.
blob = bucket.blob(blob_name)
contents = blob.download_as_string()

print(
"Downloaded storage object {} from bucket {} as the following string: {}.".format(
blob_name, bucket_name, contents
)
)


# [END storage_download_file]

if __name__ == "__main__":
download_blob_into_memory(
bucket_name=sys.argv[1],
blob_name=sys.argv[2],
)
52 changes: 52 additions & 0 deletions samples/snippets/storage_upload_from_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python

# Copyright 2021 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.

import sys

# [START storage_file_upload_from_memory]
from google.cloud import storage


def upload_blob_from_memory(bucket_name, contents, destination_blob_name):
"""Uploads a file to the bucket."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The contents to upload to the file
# contents = "these are my contents"
# The ID of your GCS object
# destination_blob_name = "storage-object-name"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)

blob.upload_from_string(contents)

print(
"{} with contents {} uploaded to {}.".format(
destination_blob_name, contents, destination_blob_name
)
)


# [END storage_file_upload_from_memory]

if __name__ == "__main__":
upload_blob_from_memory(
bucket_name=sys.argv[1],
contents=sys.argv[2],
destination_blob_name=sys.argv[3],
)

0 comments on commit b841482

Please sign in to comment.