diff --git a/storage/cloud-client/customer_supplied_keys.py b/storage/cloud-client/customer_supplied_keys.py index 1e9e6f88830e..0ac70266fe9b 100644 --- a/storage/cloud-client/customer_supplied_keys.py +++ b/storage/cloud-client/customer_supplied_keys.py @@ -43,10 +43,7 @@ ENCRYPTION_KEY = os.urandom(32) -def upload_object(storage_client, - bucket_name, - filename, - object_name, +def upload_object(storage_client, bucket_name, filename, object_name, encryption_key): """Uploads an object, specifying a custom encryption key. @@ -60,14 +57,10 @@ def upload_object(storage_client, """ bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(object_name) - with open(filename, 'rb') as f: - blob.upload_from_file(f, encryption_key=encryption_key) + blob.upload_from_filename(filename, encryption_key=encryption_key) -def download_object(storage_client, - bucket_name, - object_name, - filename, +def download_object(storage_client, bucket_name, object_name, filename, encryption_key): """Downloads an object protected by a custom encryption key. @@ -81,8 +74,7 @@ def download_object(storage_client, """ bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(object_name) - with open(filename, 'wb') as f: - blob.download_to_file(f, encryption_key=encryption_key) + blob.download_to_filename(filename, encryption_key=encryption_key) def main(bucket, filename): @@ -98,8 +90,8 @@ def main(bucket, filename): object_name=filename, filename=tmpfile.name, encryption_key=ENCRYPTION_KEY) - assert filecmp.cmp(filename, tmpfile.name), \ - 'Downloaded file has different content from the original file.' + assert filecmp.cmp(filename, tmpfile.name), ( + 'Downloaded file has different content from the original file.') print('Done') diff --git a/storage/cloud-client/manage_blobs.py b/storage/cloud-client/manage_blobs.py new file mode 100644 index 000000000000..82dfc03837e1 --- /dev/null +++ b/storage/cloud-client/manage_blobs.py @@ -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) diff --git a/storage/cloud-client/manage_blobs_test.py b/storage/cloud-client/manage_blobs_test.py new file mode 100644 index 000000000000..972d939aec83 --- /dev/null +++ b/storage/cloud-client/manage_blobs_test.py @@ -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) diff --git a/storage/cloud-client/requirements.txt b/storage/cloud-client/requirements.txt new file mode 100644 index 000000000000..868847aebf3e --- /dev/null +++ b/storage/cloud-client/requirements.txt @@ -0,0 +1 @@ +gcloud==0.17.0