|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All rights reserved. |
| 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 app demonstrating customer-supplied encryption keys. |
| 17 | +
|
| 18 | +This sample demonstrates uploading an object while supplying an encryption key, |
| 19 | +and retrieving that object's contents using gcloud API. The sample uses |
| 20 | +the default credential and project. To review their values, run this command: |
| 21 | + $ gcloud info |
| 22 | +
|
| 23 | +This sample is used on this page: |
| 24 | + https://cloud.google.com/storage/docs/encryption#customer-supplied |
| 25 | +
|
| 26 | +For more information, see the README.md under /storage. |
| 27 | +""" |
| 28 | + |
| 29 | +import argparse |
| 30 | +import base64 |
| 31 | +import filecmp |
| 32 | +import os |
| 33 | +import tempfile |
| 34 | + |
| 35 | +from gcloud import storage |
| 36 | + |
| 37 | +# An AES256 encryption key. It must be exactly 256 bits (32 bytes). You can |
| 38 | +# (and should) generate your own encryption key. os.urandom(32) is a good way |
| 39 | +# to accomplish this with Python. |
| 40 | +# |
| 41 | +# Although these keys are provided here for simplicity, please remember |
| 42 | +# that it is a bad idea to store your encryption keys in your source code. |
| 43 | +ENCRYPTION_KEY = os.urandom(32) |
| 44 | + |
| 45 | + |
| 46 | +def upload_object(storage_client, |
| 47 | + bucket_name, |
| 48 | + filename, |
| 49 | + object_name, |
| 50 | + encryption_key): |
| 51 | + """Uploads an object, specifying a custom encryption key. |
| 52 | +
|
| 53 | + Args: |
| 54 | + storage_client: gcloud client to access cloud storage |
| 55 | + bucket_name: name of the destination bucket |
| 56 | + filename: name of file to be uploaded |
| 57 | + object_name: name of resulting object |
| 58 | + encryption_key: encryption key to encrypt the object, |
| 59 | + either 32 raw bytes or a string of 32 bytes. |
| 60 | + """ |
| 61 | + bucket = storage_client.get_bucket(bucket_name) |
| 62 | + blob = bucket.blob(object_name) |
| 63 | + with open(filename, 'rb') as f: |
| 64 | + blob.upload_from_file(f, encryption_key=encryption_key) |
| 65 | + |
| 66 | + |
| 67 | +def download_object(storage_client, |
| 68 | + bucket_name, |
| 69 | + object_name, |
| 70 | + filename, |
| 71 | + encryption_key): |
| 72 | + """Downloads an object protected by a custom encryption key. |
| 73 | +
|
| 74 | + Args: |
| 75 | + storage_client: gcloud client to access cloud storage |
| 76 | + bucket_name: name of the source bucket |
| 77 | + object_name: name of the object to be downloaded |
| 78 | + filename: name of the resulting file |
| 79 | + encryption_key: the encryption key that the object is encrypted by, |
| 80 | + either 32 raw bytes or a string of 32 bytes. |
| 81 | + """ |
| 82 | + bucket = storage_client.get_bucket(bucket_name) |
| 83 | + blob = bucket.blob(object_name) |
| 84 | + with open(filename, 'wb') as f: |
| 85 | + blob.download_to_file(f, encryption_key=encryption_key) |
| 86 | + |
| 87 | + |
| 88 | +def main(bucket, filename): |
| 89 | + storage_client = storage.Client() |
| 90 | + print('Uploading object gs://{}/{} using encryption key (base64 formatted)' |
| 91 | + ' {}'.format(bucket, filename, base64.encodestring(ENCRYPTION_KEY))) |
| 92 | + upload_object(storage_client, bucket, filename, filename, ENCRYPTION_KEY) |
| 93 | + print('Downloading it back') |
| 94 | + with tempfile.NamedTemporaryFile(mode='w+b') as tmpfile: |
| 95 | + download_object(storage_client, bucket, object_name=filename, |
| 96 | + filename=tmpfile.name, encryption_key=ENCRYPTION_KEY) |
| 97 | + assert filecmp.cmp(filename, tmpfile.name), \ |
| 98 | + 'Downloaded file has different content from the original file.' |
| 99 | + print('Done') |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + parser = argparse.ArgumentParser( |
| 104 | + description=__doc__, |
| 105 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 106 | + parser.add_argument('bucket', help='Your Cloud Storage bucket.') |
| 107 | + parser.add_argument('filename', help='A file to upload and download.') |
| 108 | + |
| 109 | + args = parser.parse_args() |
| 110 | + |
| 111 | + main(args.bucket, args.filename) |
0 commit comments