|
| 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) |
0 commit comments