Skip to content

Commit 25f5aab

Browse files
author
Jon Wayne Parrott
authored
Add gcloud-based storage usage samples. (#419)
1 parent 714eae8 commit 25f5aab

File tree

4 files changed

+173
-14
lines changed

4 files changed

+173
-14
lines changed

storage/cloud-client/customer_supplied_keys.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
ENCRYPTION_KEY = os.urandom(32)
4444

4545

46-
def upload_object(storage_client,
47-
bucket_name,
48-
filename,
49-
object_name,
46+
def upload_object(storage_client, bucket_name, filename, object_name,
5047
encryption_key):
5148
"""Uploads an object, specifying a custom encryption key.
5249
@@ -60,14 +57,10 @@ def upload_object(storage_client,
6057
"""
6158
bucket = storage_client.get_bucket(bucket_name)
6259
blob = bucket.blob(object_name)
63-
with open(filename, 'rb') as f:
64-
blob.upload_from_file(f, encryption_key=encryption_key)
60+
blob.upload_from_filename(filename, encryption_key=encryption_key)
6561

6662

67-
def download_object(storage_client,
68-
bucket_name,
69-
object_name,
70-
filename,
63+
def download_object(storage_client, bucket_name, object_name, filename,
7164
encryption_key):
7265
"""Downloads an object protected by a custom encryption key.
7366
@@ -81,8 +74,7 @@ def download_object(storage_client,
8174
"""
8275
bucket = storage_client.get_bucket(bucket_name)
8376
blob = bucket.blob(object_name)
84-
with open(filename, 'wb') as f:
85-
blob.download_to_file(f, encryption_key=encryption_key)
77+
blob.download_to_filename(filename, encryption_key=encryption_key)
8678

8779

8880
def main(bucket, filename):
@@ -98,8 +90,8 @@ def main(bucket, filename):
9890
object_name=filename,
9991
filename=tmpfile.name,
10092
encryption_key=ENCRYPTION_KEY)
101-
assert filecmp.cmp(filename, tmpfile.name), \
102-
'Downloaded file has different content from the original file.'
93+
assert filecmp.cmp(filename, tmpfile.name), (
94+
'Downloaded file has different content from the original file.')
10395
print('Done')
10496

10597

storage/cloud-client/manage_blobs.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright (C) 2016 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""Command-line sample application for simple CRUD management of blobs in a
17+
given bucket.
18+
19+
For more information, see the README.md under /storage.
20+
"""
21+
22+
import argparse
23+
24+
from gcloud import storage
25+
26+
27+
def list_blobs(bucket_name):
28+
"""Lists all the blobs in the bucket."""
29+
storage_client = storage.Client()
30+
bucket = storage_client.get_bucket(bucket_name)
31+
32+
blobs = bucket.list_blobs()
33+
34+
for blob in blobs:
35+
print(blob.name)
36+
37+
38+
def upload_blob(bucket_name, source_file_name, destination_blob_name):
39+
"""Uploads a file to the bucket."""
40+
storage_client = storage.Client()
41+
bucket = storage_client.get_bucket(bucket_name)
42+
blob = bucket.blob(destination_blob_name)
43+
44+
blob.upload_from_filename(source_file_name)
45+
46+
print('File {} uploaded to {}.'.format(
47+
source_file_name,
48+
destination_blob_name))
49+
50+
51+
def download_blob(bucket_name, source_blob_name, destination_file_name):
52+
"""Downloads a blob from the bucket."""
53+
storage_client = storage.Client()
54+
bucket = storage_client.get_bucket(bucket_name)
55+
blob = bucket.blob(source_blob_name)
56+
57+
blob.download_to_filename(destination_file_name)
58+
59+
print('Blob {} downloaded to {}.'.format(
60+
source_blob_name,
61+
destination_file_name))
62+
63+
64+
def delete_blob(bucket_name, blob_name):
65+
"""Deletes a blob from the bucket."""
66+
storage_client = storage.Client()
67+
bucket = storage_client.get_bucket(bucket_name)
68+
blob = bucket.blob(blob_name)
69+
70+
blob.delete()
71+
72+
print('Blob {} deleted.'.format(blob_name))
73+
74+
75+
if __name__ == '__main__':
76+
parser = argparse.ArgumentParser()
77+
parser.add_argument('bucket_name', help='Your cloud storage bucket.')
78+
79+
subparsers = parser.add_subparsers(dest='command')
80+
subparsers.add_parser('list', help=list_blobs.__doc__)
81+
82+
upload_parser = subparsers.add_parser('upload', help=upload_blob.__doc__)
83+
upload_parser.add_argument('source_file_name')
84+
upload_parser.add_argument('destination_blob_name')
85+
86+
download_parser = subparsers.add_parser(
87+
'download', help=download_blob.__doc__)
88+
download_parser.add_argument('source_blob_name')
89+
download_parser.add_argument('destination_file_name')
90+
91+
delete_parser = subparsers.add_parser('delete', help=delete_blob.__doc__)
92+
delete_parser.add_argument('blob_name')
93+
94+
args = parser.parse_args()
95+
96+
if args.command == 'list':
97+
list_blobs(args.bucket_name)
98+
elif args.command == 'upload':
99+
upload_blob(
100+
args.bucket_name,
101+
args.source_file_name,
102+
args.destination_blob_name)
103+
elif args.command == 'download':
104+
download_blob(
105+
args.bucket_name,
106+
args.source_blob_name,
107+
args.destination_file_name)
108+
elif args.command == 'delete':
109+
delete_blob(args.bucket_name, args.blob_name)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import tempfile
15+
16+
from gcloud import storage
17+
import manage_blobs
18+
import pytest
19+
20+
21+
@pytest.fixture
22+
def test_blob(cloud_config):
23+
bucket = storage.Client().bucket(cloud_config.storage_bucket)
24+
blob = bucket.blob('manage_blobs_test_sigil')
25+
blob.upload_from_string('Hello, is it me you\'re looking for?')
26+
return blob.name
27+
28+
29+
def test_list_blobs(test_blob, cloud_config, capsys):
30+
manage_blobs.list_blobs(cloud_config.storage_bucket)
31+
out, _ = capsys.readouterr()
32+
assert test_blob in out
33+
34+
35+
def test_upload_blob(cloud_config):
36+
with tempfile.NamedTemporaryFile() as source_file:
37+
source_file.write(b'test')
38+
39+
manage_blobs.upload_blob(
40+
cloud_config.storage_bucket,
41+
source_file.name, 'test_upload_blob')
42+
43+
44+
def test_download_blob(test_blob, cloud_config):
45+
with tempfile.NamedTemporaryFile() as dest_file:
46+
manage_blobs.download_blob(
47+
cloud_config.storage_bucket,
48+
test_blob,
49+
dest_file.name)
50+
51+
assert dest_file.read()
52+
53+
54+
def test_delete_blob(test_blob, cloud_config):
55+
manage_blobs.delete_blob(
56+
cloud_config.storage_bucket,
57+
test_blob)

storage/cloud-client/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud==0.17.0

0 commit comments

Comments
 (0)