Skip to content

Commit

Permalink
Add gcloud-based storage usage samples.
Browse files Browse the repository at this point in the history
Change-Id: Ie2e2658c4dd74d76d2664c8ff830adfcf6d0d831
  • Loading branch information
Jon Wayne Parrott committed Jul 13, 2016
1 parent 1e00a2c commit bcc7261
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 14 deletions.
20 changes: 6 additions & 14 deletions storage/cloud-client/customer_supplied_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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):
Expand All @@ -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')


Expand Down
109 changes: 109 additions & 0 deletions storage/cloud-client/manage_blobs.py
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)
57 changes: 57 additions & 0 deletions storage/cloud-client/manage_blobs_test.py
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)
1 change: 1 addition & 0 deletions storage/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcloud==0.17.0

0 comments on commit bcc7261

Please sign in to comment.