-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gcloud-based storage usage samples. #419
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,109 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright (C) 2016 Google Inc. | ||
# | ||
# 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. | ||
"""Command-line sample application for simple CRUD management of blobs in a | ||
given bucket. | ||
|
||
For more information, see the README.md under /storage. | ||
""" | ||
|
||
import argparse | ||
|
||
from gcloud import storage | ||
|
||
|
||
def list_blobs(bucket_name): | ||
"""Lists all the blobs in the bucket.""" | ||
storage_client = storage.Client() | ||
bucket = storage_client.get_bucket(bucket_name) | ||
|
||
blobs = bucket.list_blobs() | ||
|
||
for blob in blobs: | ||
print(blob.name) | ||
|
||
|
||
def upload_blob(bucket_name, source_file_name, destination_blob_name): | ||
"""Uploads a file to the bucket.""" | ||
storage_client = storage.Client() | ||
bucket = storage_client.get_bucket(bucket_name) | ||
blob = bucket.blob(destination_blob_name) | ||
|
||
blob.upload_from_filename(source_file_name) | ||
|
||
print('File {} uploaded to {}.'.format( | ||
source_file_name, | ||
destination_blob_name)) | ||
|
||
|
||
def download_blob(bucket_name, source_blob_name, destination_file_name): | ||
"""Downloads a blob from the bucket.""" | ||
storage_client = storage.Client() | ||
bucket = storage_client.get_bucket(bucket_name) | ||
blob = bucket.blob(source_blob_name) | ||
|
||
blob.download_to_filename(destination_file_name) | ||
|
||
print('Blob {} downloaded to {}.'.format( | ||
source_blob_name, | ||
destination_file_name)) | ||
|
||
|
||
def delete_blob(bucket_name, blob_name): | ||
"""Deletes a blob from the bucket.""" | ||
storage_client = storage.Client() | ||
bucket = storage_client.get_bucket(bucket_name) | ||
blob = bucket.blob(blob_name) | ||
|
||
blob.delete() | ||
|
||
print('Blob {} deleted.'.format(blob_name)) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('bucket_name', help='Your cloud storage bucket.') | ||
|
||
subparsers = parser.add_subparsers(dest='command') | ||
subparsers.add_parser('list', help=list_blobs.__doc__) | ||
|
||
upload_parser = subparsers.add_parser('upload', help=upload_blob.__doc__) | ||
upload_parser.add_argument('source_file_name') | ||
upload_parser.add_argument('destination_blob_name') | ||
|
||
download_parser = subparsers.add_parser( | ||
'download', help=download_blob.__doc__) | ||
download_parser.add_argument('source_blob_name') | ||
download_parser.add_argument('destination_file_name') | ||
|
||
delete_parser = subparsers.add_parser('delete', help=delete_blob.__doc__) | ||
delete_parser.add_argument('blob_name') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == 'list': | ||
list_blobs(args.bucket_name) | ||
elif args.command == 'upload': | ||
upload_blob( | ||
args.bucket_name, | ||
args.source_file_name, | ||
args.destination_blob_name) | ||
elif args.command == 'download': | ||
download_blob( | ||
args.bucket_name, | ||
args.source_blob_name, | ||
args.destination_file_name) | ||
elif args.command == 'delete': | ||
delete_blob(args.bucket_name, args.blob_name) |
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,57 @@ | ||
# Copyright 2016, Google, Inc. | ||
# 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 tempfile | ||
|
||
from gcloud import storage | ||
import manage_blobs | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def test_blob(cloud_config): | ||
bucket = storage.Client().bucket(cloud_config.storage_bucket) | ||
blob = bucket.blob('manage_blobs_test_sigil') | ||
blob.upload_from_string('Hello, is it me you\'re looking for?') | ||
return blob.name | ||
|
||
|
||
def test_list_blobs(test_blob, cloud_config, capsys): | ||
manage_blobs.list_blobs(cloud_config.storage_bucket) | ||
out, _ = capsys.readouterr() | ||
assert test_blob in out | ||
|
||
|
||
def test_upload_blob(cloud_config): | ||
with tempfile.NamedTemporaryFile() as source_file: | ||
source_file.write(b'test') | ||
|
||
manage_blobs.upload_blob( | ||
cloud_config.storage_bucket, | ||
source_file.name, 'test_upload_blob') | ||
|
||
|
||
def test_download_blob(test_blob, cloud_config): | ||
with tempfile.NamedTemporaryFile() as dest_file: | ||
manage_blobs.download_blob( | ||
cloud_config.storage_bucket, | ||
test_blob, | ||
dest_file.name) | ||
|
||
assert dest_file.read() | ||
|
||
|
||
def test_delete_blob(test_blob, cloud_config): | ||
manage_blobs.delete_blob( | ||
cloud_config.storage_bucket, | ||
test_blob) |
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 @@ | ||
gcloud==0.17.0 |
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.
I wonder if str(blog) should just be blob.name
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.
Not sure what you mean?