Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 93b43d5

Browse filesBrowse files
author
Jon Wayne Parrott
authored
Add bucket label samples (GoogleCloudPlatform#1045)
1 parent 0ca81d0 commit 93b43d5
Copy full SHA for 93b43d5

File tree

Expand file treeCollapse file tree

2 files changed

+71
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-0
lines changed

‎storage/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: storage/cloud-client/snippets.py
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import argparse
2525
import datetime
26+
import pprint
2627

2728
from google.cloud import storage
2829

@@ -42,6 +43,45 @@ def delete_bucket(bucket_name):
4243
print('Bucket {} deleted'.format(bucket.name))
4344

4445

46+
def get_bucket_labels(bucket_name):
47+
"""Prints out a bucket's labels."""
48+
storage_client = storage.Client()
49+
bucket = storage_client.get_bucket(bucket_name)
50+
labels = bucket.labels
51+
pprint.pprint(labels)
52+
53+
54+
def add_bucket_label(bucket_name):
55+
"""Add a label to a bucket."""
56+
storage_client = storage.Client()
57+
bucket = storage_client.get_bucket(bucket_name)
58+
59+
labels = bucket.labels
60+
labels['example'] = 'label'
61+
bucket.labels = labels
62+
bucket.patch()
63+
64+
print('Updated labels on {}.'.format(bucket.name))
65+
pprint.pprint(bucket.labels)
66+
67+
68+
def remove_bucket_label(bucket_name):
69+
"""Remove a label from a bucket."""
70+
storage_client = storage.Client()
71+
bucket = storage_client.get_bucket(bucket_name)
72+
73+
labels = bucket.labels
74+
75+
if 'example' in labels:
76+
del labels['example']
77+
78+
bucket.labels = labels
79+
bucket.patch()
80+
81+
print('Updated labels on {}.'.format(bucket.name))
82+
pprint.pprint(bucket.labels)
83+
84+
4585
def list_blobs(bucket_name):
4686
"""Lists all the blobs in the bucket."""
4787
storage_client = storage.Client()
@@ -222,6 +262,10 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
222262
subparsers = parser.add_subparsers(dest='command')
223263
subparsers.add_parser('create-bucket', help=create_bucket.__doc__)
224264
subparsers.add_parser('delete-bucket', help=delete_bucket.__doc__)
265+
subparsers.add_parser('get-bucket-labels', help=get_bucket_labels.__doc__)
266+
subparsers.add_parser('add-bucket-label', help=add_bucket_label.__doc__)
267+
subparsers.add_parser(
268+
'remove-bucket-label', help=remove_bucket_label.__doc__)
225269
subparsers.add_parser('list', help=list_blobs.__doc__)
226270

227271
list_with_prefix_parser = subparsers.add_parser(
@@ -268,6 +312,12 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
268312
create_bucket(args.bucket_name)
269313
elif args.command == 'delete-bucket':
270314
delete_bucket(args.bucket_name)
315+
if args.command == 'get-bucket-labels':
316+
get_bucket_labels(args.bucket_name)
317+
if args.command == 'add-bucket-label':
318+
add_bucket_label(args.bucket_name)
319+
if args.command == 'remove-bucket-label':
320+
remove_bucket_label(args.bucket_name)
271321
elif args.command == 'list':
272322
list_blobs(args.bucket_name)
273323
elif args.command == 'list-with-prefix':

‎storage/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: storage/cloud-client/snippets_test.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@
2525
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
2626

2727

28+
def test_get_bucket_labels():
29+
snippets.get_bucket_labels(BUCKET)
30+
31+
32+
def test_add_bucket_label(capsys):
33+
snippets.add_bucket_label(BUCKET)
34+
out, _ = capsys.readouterr()
35+
assert 'example' in out
36+
37+
38+
@pytest.mark.xfail(
39+
reason=(
40+
'https://github.com/GoogleCloudPlatform'
41+
'/google-cloud-python/issues/3711'))
42+
def test_remove_bucket_label(capsys):
43+
snippets.add_bucket_label(BUCKET)
44+
snippets.remove_bucket_label(BUCKET)
45+
out, _ = capsys.readouterr()
46+
assert '{}' in out
47+
48+
2849
@pytest.fixture
2950
def test_blob():
3051
"""Provides a pre-existing blob in the test bucket."""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.