8000 Bucket metadata sample (#2414) · easterday/python-docs-samples@5d43edb · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d43edb

Browse files
Bucket metadata sample (GoogleCloudPlatform#2414)
* Remove required argument from list buckets sample * Bucket metadata sample * Bucket metadata sample
1 parent 27eddc7 commit 5d43edb

File tree

7 files changed

+73
-11
lines changed

7 files changed

+73
-11
lines changed

storage/cloud-client/README.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ To run this sample:
9393
$ python snippets.py
9494
9595
usage: snippets.py [-h]
96-
{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}
96+
{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}
9797
...
9898
9999
This application demonstrates how to perform basic operations on blobs
@@ -103,7 +103,7 @@ To run this sample:
103103
at https://cloud.google.com/storage/docs.
104104
105105
positional arguments:
106-
{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}
106+
{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}
107107
list-buckets Lists all buckets.
108108
create-bucket Creates a new bucket.
109109
delete-bucket Deletes a bucket. The bucket must be empty.
@@ -112,6 +112,7 @@ To run this sample:
112112
remove-bucket-label
113113
Remove a label from a bucket.
114114
list Lists all the blobs in the bucket.
115+
bucket-metadata Prints out a bucket's metadata.
115116
list-with-prefix Lists all the blobs in the bucket that begin with the
116117
prefix. This can be used to list all blobs in a
117118
"folder", e.g. "public/". The delimiter argument can
@@ -403,6 +404,22 @@ To run this sample:
403404
404405
405406
407+
Service Account HMAC Keys
408+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
409+
410+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
411+
: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
412+
413+
414+
415+
416+
To run this sample:
417+
418+
.. code-block:: bash
419+
420+
$ python hmac_samples.py
421+
422+
406423
407424
408425
The client library

storage/cloud-client/README.rst.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ samples:
3333
- name: Notification Polling
3434
file: notification_polling.py
3535
show_help: true
36+
- name: Service Account HMAC Keys
37+
file: hmac_samples.py
38+
show_help: false
3639

3740
cloud_client_library: true
3841

39-
folder: storage/cloud-client
42+
folder: storage/cloud-client
File renamed without changes.

storage/cloud-client/hmac_test.py renamed to storage/cloud-client/hmac_samples_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from google.cloud import storage
2424
import pytest
2525

26-
import hmac
26+
import hmac_samples
2727

2828

2929
PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
@@ -54,13 +54,13 @@ def new_hmac_key():
5454

5555

5656
def test_list_keys(capsys, new_hmac_key):
57-
hmac_keys = hmac.list_keys(PROJECT_ID)
57+
hmac_keys = hmac_samples.list_keys(PROJECT_ID)
5858
assert 'HMAC Keys:' in capsys.readouterr().out
5959
assert hmac_keys.num_results >= 1
6060

6161

6262
def test_create_key(capsys):
63-
hmac_key = hmac.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL)
63+
hmac_key = hmac_samples.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL)
6464
hmac_key.state = 'INACTIVE'
6565
hmac_key.update()
6666
hmac_key.delete()
@@ -69,27 +69,27 @@ def test_create_key(capsys):
6969

7070

7171
def test_get_key(capsys, new_hmac_key):
72-
hmac_key = hmac.get_key(new_hmac_key.access_id, PROJECT_ID)
72+
hmac_key = hmac_samples.get_key(new_hmac_key.access_id, PROJECT_ID)
7373
assert 'HMAC key metadata' in capsys.readouterr().out
7474
assert hmac_key.access_id == new_hmac_key.access_id
7575

7676

7777
def test_activate_key(capsys, new_hmac_key):
7878
new_hmac_key.state = 'INACTIVE'
7979
new_hmac_key.update()
80-
hmac_key = hmac.activate_key(new_hmac_key.access_id, PROJECT_ID)
80+
hmac_key = hmac_samples.activate_key(new_hmac_key.access_id, PROJECT_ID)
8181
assert 'State: ACTIVE' in capsys.readouterr().out
8282
assert hmac_key.state == 'ACTIVE'
8383

8484

8585
def test_deactivate_key(capsys, new_hmac_key):
86-
hmac_key = hmac.deactivate_key(new_hmac_key.access_id, PROJECT_ID)
86+
hmac_key = hmac_samples.deactivate_key(new_hmac_key.access_id, PROJECT_ID)
8787
assert 'State: INACTIVE' in capsys.readouterr().out
8888
assert hmac_key.state == 'INACTIVE'
8989

9090

9191
def test_delete_key(capsys, new_hmac_key):
9292
new_hmac_key.state = 'INACTIVE'
9393
new_hmac_key.update()
94-
hmac.delete_key(new_hmac_key.access_id, PROJECT_ID)
94+
hmac_samples.delete_key(new_hmac_key.access_id, PROJECT_ID)
9595
assert 'The key is deleted' in capsys.readouterr().out

storage/cloud-client/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
google-cloud-pubsub==0.39.1
2-
google-cloud-storage==1.19.0
2+
google-cloud-storage==1.19.1

storage/cloud-client/snippets.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,35 @@ def blob_metadata(bucket_name, blob_name):
248248
.format(blob.retention_expiration_time))
249249

250250

251+
def bucket_metadata(bucket_name):
252+
"""Prints out a bucket's metadata."""
253+
# [START storage_get_bucket_metadata]
254+
storage_client = storage.Client()
255+
bucket = storage_client.get_bucket(bucket_name)
256+
257+
print('ID: {}'.format(bucket.id))
258+
print('Name: {}'.format(bucket.name))
259+
print('Storage Class: {}'.format(bucket.storage_class))
260+
print('Location: {}'.format(bucket.location))
261+
print('Location Type: {}'.format(bucket.location_type))
262+
print('Cors: {}'.format(bucket.cors))
263+
print('Default Event Based Hold: {}'
264+
.format(bucket.default_event_based_hold))
265+
print('Default KMS Key Name: {}'.format(bucket.default_kms_key_name))
266+
print('Metageneration: {}'.format(bucket.metageneration))
267+
print('Retention Effective Time: {}'
268+
.format(bucket.retention_policy_effective_time))
269+
print('Retention Period: {}'.format(bucket.retention_period))
270+
print('Retention Policy Locked: {}'.format(bucket.retention_policy_locked))
271+
print('Requester Pays: {}'.format(bucket.requester_pays))
272+
print('Self Link: {}'.format(bucket.self_link))
273+
print('Time Created: {}'.format(bucket.time_created))
274+
print('Versioning Enabled: {}'.format(bucket.versioning_enabled))
275+
print('Labels:')
276+
pprint.pprint(bucket.labels)
277+
# [END storage_get_bucket_metadata]
278+
279+
251280
def make_blob_public(bucket_name, blob_name):
252281
"""Makes a blob publicly accessible."""
253282
storage_client = storage.Client()
@@ -367,6 +396,8 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
367396
def bucket_commands(args):
368397
if args.command == 'list-buckets':
369398
list_buckets()
399+
elif args.command == 'bucket-metadata':
400+
bucket_metadata(args.bucket_name)
370401
elif args.command == 'create-bucket':
371402
create_bucket(args.bucket_name)
372403
elif args.command == 'enable-default-kms-key':
@@ -464,6 +495,11 @@ def main():
464495
list_blobs_parser.add_argument(
465496
'bucket_name', help='Your cloud storage bucket.')
466497

498+
bucket_metadata_parser = subparsers.add_parser(
499+
'bucket-metadata', help=bucket_metadata.__doc__)
500+
bucket_metadata_parser.add_argument(
501+
'bucket_name', help='Your cloud storage bucket.')
502+
467503
list_with_prefix_parser = subparsers.add_parser(
468504
'list-with-prefix', help=list_blobs_with_prefix.__doc__)
469505
list_with_prefix_parser.add_argument(

storage/cloud-client/snippets_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ def test_list_blobs(test_blob, capsys):
7676
assert test_blob.name in out
7777

7878

79+
def test_bucket_metadata(capsys):
80+
snippets.bucket_metadata(BUCKET)
81+
out, _ = capsys.readouterr()
82+
assert BUCKET in out
83+
84+
7985
def test_list_blobs_with_prefix(test_blob, capsys):
8086
snippets.list_blobs_with_prefix(
8187
BUCKET,

0 commit comments

Comments
 (0)
0