diff --git a/storage/cloud-client/README.rst b/storage/cloud-client/README.rst index ef32d53de96..48580f661c0 100644 --- a/storage/cloud-client/README.rst +++ b/storage/cloud-client/README.rst @@ -93,7 +93,7 @@ To run this sample: $ python snippets.py usage: snippets.py [-h] - {list-buckets,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,signed-url-download-v4,signed-url-upload-v4,rename,copy} + {list-buckets,create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,bucket-metadata,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,signed-url-download-v4,signed-url-upload-v4,rename,copy} ... This application demonstrates how to perform basic operations on blobs @@ -103,7 +103,7 @@ To run this sample: at https://cloud.google.com/storage/docs. positional arguments: - {list-buckets,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,signed-url-download-v4,signed-url-upload-v4,rename,copy} + {list-buckets,create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,bucket-metadata,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,signed-url-download-v4,signed-url-upload-v4,rename,copy} list-buckets Lists all buckets. create-bucket Creates a new bucket. delete-bucket Deletes a bucket. The bucket must be empty. @@ -112,6 +112,7 @@ To run this sample: remove-bucket-label Remove a label from a bucket. list Lists all the blobs in the bucket. + bucket-metadata Prints out a bucket's metadata. list-with-prefix Lists all the blobs in the bucket that begin with the prefix. This can be used to list all blobs in a "folder", e.g. "public/". The delimiter argument can @@ -403,6 +404,22 @@ To run this sample: +Service Account HMAC Keys ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/cloud-client/hmac_samples.py,storage/cloud-client/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python hmac_samples.py + + The client library diff --git a/storage/cloud-client/README.rst.in b/storage/cloud-client/README.rst.in index 3b8f33af7f5..346c6a0d8e8 100644 --- a/storage/cloud-client/README.rst.in +++ b/storage/cloud-client/README.rst.in @@ -33,7 +33,10 @@ samples: - name: Notification Polling file: notification_polling.py show_help: true +- name: Service Account HMAC Keys + file: hmac_samples.py + show_help: false cloud_client_library: true -folder: storage/cloud-client \ No newline at end of file +folder: storage/cloud-client diff --git a/storage/cloud-client/hmac.py b/storage/cloud-client/hmac_samples.py similarity index 100% rename from storage/cloud-client/hmac.py rename to storage/cloud-client/hmac_samples.py diff --git a/storage/cloud-client/hmac_test.py b/storage/cloud-client/hmac_samples_test.py similarity index 85% rename from storage/cloud-client/hmac_test.py rename to storage/cloud-client/hmac_samples_test.py index 0e936467c17..9d67f9ef7c3 100644 --- a/storage/cloud-client/hmac_test.py +++ b/storage/cloud-client/hmac_samples_test.py @@ -23,7 +23,7 @@ from google.cloud import storage import pytest -import hmac +import hmac_samples PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] @@ -54,13 +54,13 @@ def new_hmac_key(): def test_list_keys(capsys, new_hmac_key): - hmac_keys = hmac.list_keys(PROJECT_ID) + hmac_keys = hmac_samples.list_keys(PROJECT_ID) assert 'HMAC Keys:' in capsys.readouterr().out assert hmac_keys.num_results >= 1 def test_create_key(capsys): - hmac_key = hmac.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL) + hmac_key = hmac_samples.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL) hmac_key.state = 'INACTIVE' hmac_key.update() hmac_key.delete() @@ -69,7 +69,7 @@ def test_create_key(capsys): def test_get_key(capsys, new_hmac_key): - hmac_key = hmac.get_key(new_hmac_key.access_id, PROJECT_ID) + hmac_key = hmac_samples.get_key(new_hmac_key.access_id, PROJECT_ID) assert 'HMAC key metadata' in capsys.readouterr().out assert hmac_key.access_id == new_hmac_key.access_id @@ -77,13 +77,13 @@ def test_get_key(capsys, new_hmac_key): def test_activate_key(capsys, new_hmac_key): new_hmac_key.state = 'INACTIVE' new_hmac_key.update() - hmac_key = hmac.activate_key(new_hmac_key.access_id, PROJECT_ID) + hmac_key = hmac_samples.activate_key(new_hmac_key.access_id, PROJECT_ID) assert 'State: ACTIVE' in capsys.readouterr().out assert hmac_key.state == 'ACTIVE' def test_deactivate_key(capsys, new_hmac_key): - hmac_key = hmac.deactivate_key(new_hmac_key.access_id, PROJECT_ID) + hmac_key = hmac_samples.deactivate_key(new_hmac_key.access_id, PROJECT_ID) assert 'State: INACTIVE' in capsys.readouterr().out assert hmac_key.state == 'INACTIVE' @@ -91,5 +91,5 @@ def test_deactivate_key(capsys, new_hmac_key): def test_delete_key(capsys, new_hmac_key): new_hmac_key.state = 'INACTIVE' new_hmac_key.update() - hmac.delete_key(new_hmac_key.access_id, PROJECT_ID) + hmac_samples.delete_key(new_hmac_key.access_id, PROJECT_ID) assert 'The key is deleted' in capsys.readouterr().out diff --git a/storage/cloud-client/requirements.txt b/storage/cloud-client/requirements.txt index a4682fd3bfb..a498968ff2e 100644 --- a/storage/cloud-client/requirements.txt +++ b/storage/cloud-client/requirements.txt @@ -1,2 +1,2 @@ google-cloud-pubsub==0.39.1 -google-cloud-storage==1.19.0 +google-cloud-storage==1.19.1 diff --git a/storage/cloud-client/snippets.py b/storage/cloud-client/snippets.py index 656c6ebb185..a184b5323d4 100644 --- a/storage/cloud-client/snippets.py +++ b/storage/cloud-client/snippets.py @@ -248,6 +248,35 @@ def blob_metadata(bucket_name, blob_name): .format(blob.retention_expiration_time)) +def bucket_metadata(bucket_name): + """Prints out a bucket's metadata.""" + # [START storage_get_bucket_metadata] + storage_client = storage.Client() + bucket = storage_client.get_bucket(bucket_name) + + print('ID: {}'.format(bucket.id)) + print('Name: {}'.format(bucket.name)) + print('Storage Class: {}'.format(bucket.storage_class)) + print('Location: {}'.format(bucket.location)) + print('Location Type: {}'.format(bucket.location_type)) + print('Cors: {}'.format(bucket.cors)) + print('Default Event Based Hold: {}' + .format(bucket.default_event_based_hold)) + print('Default KMS Key Name: {}'.format(bucket.default_kms_key_name)) + print('Metageneration: {}'.format(bucket.metageneration)) + print('Retention Effective Time: {}' + .format(bucket.retention_policy_effective_time)) + print('Retention Period: {}'.format(bucket.retention_period)) + print('Retention Policy Locked: {}'.format(bucket.retention_policy_locked)) + print('Requester Pays: {}'.format(bucket.requester_pays)) + print('Self Link: {}'.format(bucket.self_link)) + print('Time Created: {}'.format(bucket.time_created)) + print('Versioning Enabled: {}'.format(bucket.versioning_enabled)) + print('Labels:') + pprint.pprint(bucket.labels) + # [END storage_get_bucket_metadata] + + def make_blob_public(bucket_name, blob_name): """Makes a blob publicly accessible.""" storage_client = storage.Client() @@ -367,6 +396,8 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name): def bucket_commands(args): if args.command == 'list-buckets': list_buckets() + elif args.command == 'bucket-metadata': + bucket_metadata(args.bucket_name) elif args.command == 'create-bucket': create_bucket(args.bucket_name) elif args.command == 'enable-default-kms-key': @@ -464,6 +495,11 @@ def main(): list_blobs_parser.add_argument( 'bucket_name', help='Your cloud storage bucket.') + bucket_metadata_parser = subparsers.add_parser( + 'bucket-metadata', help=bucket_metadata.__doc__) + bucket_metadata_parser.add_argument( + 'bucket_name', help='Your cloud storage bucket.') + list_with_prefix_parser = subparsers.add_parser( 'list-with-prefix', help=list_blobs_with_prefix.__doc__) list_with_prefix_parser.add_argument( diff --git a/storage/cloud-client/snippets_test.py b/storage/cloud-client/snippets_test.py index 117bbd5bb06..a03609743ec 100644 --- a/storage/cloud-client/snippets_test.py +++ b/storage/cloud-client/snippets_test.py @@ -76,6 +76,12 @@ def test_list_blobs(test_blob, capsys): assert test_blob.name in out +def test_bucket_metadata(capsys): + snippets.bucket_metadata(BUCKET) + out, _ = capsys.readouterr() + assert BUCKET in out + + def test_list_blobs_with_prefix(test_blob, capsys): snippets.list_blobs_with_prefix( BUCKET,