Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions storage/cloud-client/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion storage/cloud-client/README.rst.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
folder: storage/cloud-client
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from google.cloud import storage
import pytest

import hmac
import hmac_samples


PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
Expand Down Expand Up @@ -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()
Expand All @@ -69,27 +69,27 @@ 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


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'


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
2 changes: 1 addition & 1 deletion storage/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-pubsub==0.39.1
google-cloud-storage==1.19.0
google-cloud-storage==1.19.1
36 changes: 36 additions & 0 deletions storage/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions storage/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down