-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: added upload from/download into memory samples (#664)
* 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
1 parent
0459cb4
commit b841482
Showing
4 changed files
with
147 additions
and
0 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 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 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,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], | ||
) |
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,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], | ||
) |