Skip to content

Commit 364d3aa

Browse files
broadyandrewsg
authored andcommitted
Storage: add KMS samples (GoogleCloudPlatform#1510)
* Storage: add KMS samples * Add CLOUD_KMS_KEY environment variable
1 parent b8da199 commit 364d3aa

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

storage/cloud-client/README.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ To run this sample:
9494
9595
usage: snippets.py [-h]
9696
bucket_name
97-
{create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,list-with-prefix,upload,download,delete,metadata,make-public,signed-url,rename,copy}
97+
{create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,rename,copy}
9898
...
9999
100100
This application demonstrates how to perform basic operations on blobs
@@ -105,7 +105,7 @@ To run this sample:
105105
106106
positional arguments:
107107
bucket_name Your cloud storage bucket.
108-
{create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,list-with-prefix,upload,download,delete,metadata,make-public,signed-url,rename,copy}
108+
{create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,rename,copy}
109109
create-bucket Creates a new bucket.
110110
delete-bucket Deletes a bucket. The bucket must be empty.
111111
get-bucket-labels Prints out a bucket's labels.
@@ -124,6 +124,11 @@ To run this sample:
124124
However, if you specify prefix='/a' and delimiter='/',
125125
you'll get back: /a/1.txt
126126
upload Uploads a file to the bucket.
127+
enable-default-kms-key
128+
Sets a bucket's default KMS key.
129+
upload-with-kms-key
130+
Uploads a file to the bucket, encrypting it with the
131+
given KMS key.
127132
download Downloads a blob from the bucket.
128133
delete Deletes a blob from the bucket.
129134
metadata Prints out a blob's metadata.

storage/cloud-client/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-storage==1.8.0
1+
google-cloud-storage==1.10.0
22
google-cloud-pubsub==0.32.1

storage/cloud-client/snippets.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ def delete_bucket(bucket_name):
4343
print('Bucket {} deleted'.format(bucket.name))
4444

4545

46+
def enable_default_kms_key(bucket_name, kms_key_name):
47+
"""Sets a bucket's default KMS key."""
48+
storage_client = storage.Client()
49+
bucket = storage_client.get_bucket(bucket_name)
50+
bucket.default_kms_key_name = kms_key_name
51+
bucket.patch()
52+
53+
print('Set default KMS key for bucket {} to {}.'.format(
54+
bucket.name,
55+
bucket.default_kms_key_name))
56+
57+
4658
def get_bucket_labels(bucket_name):
4759
"""Prints out a bucket's labels."""
4860
storage_client = storage.Client()
@@ -143,6 +155,20 @@ def upload_blob(bucket_name, source_file_name, destination_blob_name):
143155
destination_blob_name))
144156

145157

158+
def upload_blob_with_kms(bucket_name, source_file_name, destination_blob_name,
159+
kms_key_name):
160+
"""Uploads a file to the bucket, encrypting it with the given KMS key."""
161+
storage_client = storage.Client()
162+
bucket = storage_client.get_bucket(bucket_name)
163+
blob = bucket.blob(destination_blob_name, kms_key_name=kms_key_name)
164+
blob.upload_from_filename(source_file_name)
165+
166+
print('File {} uploaded to {} with encryption key {}.'.format(
167+
source_file_name,
168+
destination_blob_name,
169+
kms_key_name))
170+
171+
146172
def download_blob(bucket_name, source_blob_name, destination_file_name):
147173
"""Downloads a blob from the bucket."""
148174
storage_client = storage.Client()
@@ -277,6 +303,16 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
277303
upload_parser.add_argument('source_file_name')
278304
upload_parser.add_argument('destination_blob_name')
279305

306+
enable_default_kms_parser = subparsers.add_parser(
307+
'enable-default-kms-key', help=enable_default_kms_key.__doc__)
308+
enable_default_kms_parser.add_argument('kms_key_name')
309+
310+
upload_kms_parser = subparsers.add_parser(
311+
'upload-with-kms-key', help=upload_blob_with_kms.__doc__)
312+
upload_kms_parser.add_argument('source_file_name')
313+
upload_kms_parser.add_argument('destination_blob_name')
314+
upload_kms_parser.add_argument('kms_key_name')
315+
280316
download_parser = subparsers.add_parser(
281317
'download', help=download_blob.__doc__)
282318
download_parser.add_argument('source_blob_name')
@@ -310,6 +346,8 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
310346

311347
if args.command == 'create-bucket':
312348
create_bucket(args.bucket_name)
349+
if args.command == 'enable-default-kms-key':
350+
enable_default_kms_key(args.bucket_name, args.kms_key_name)
313351
elif args.command == 'delete-bucket':
314352
delete_bucket(args.bucket_name)
315353
if args.command == 'get-bucket-labels':
@@ -327,6 +365,12 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
327365
args.bucket_name,
328366
args.source_file_name,
329367
args.destination_blob_name)
368+
elif args.command == 'upload-with-kms-key':
369+
upload_blob_with_kms(
370+
args.bucket_name,
371+
args.source_file_name,
372+
args.destination_blob_name,
373+
args.kms_key_name)
330374
elif args.command == 'download':
331375
download_blob(
332376
args.bucket_name,

storage/cloud-client/snippets_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import snippets
2424

2525
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
26+
KMS_KEY = os.environ['CLOUD_KMS_KEY']
2627

2728

2829
def test_get_bucket_labels():
@@ -79,6 +80,17 @@ def test_upload_blob():
7980
'test_upload_blob')
8081

8182

83+
def test_upload_blob_with_kms():
84+
with tempfile.NamedTemporaryFile() as source_file:
85+
source_file.write(b'test')
86+
87+
snippets.upload_blob_with_kms(
88+
BUCKET,
89+
source_file.name,
90+
'test_upload_blob_encrypted',
91+
KMS_KEY)
92+
93+
8294
def test_download_blob(test_blob):
8395
with tempfile.NamedTemporaryFile() as dest_file:
8496
snippets.download_blob(

testing/secrets.tar.enc

-3.52 KB
Binary file not shown.

0 commit comments

Comments
 (0)