2323
2424import argparse
2525import datetime
26+ import pprint
2627
2728from 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+
4585def 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' :
0 commit comments