23
23
24
24
import argparse
25
25
import datetime
26
+ import pprint
26
27
27
28
from google .cloud import storage
28
29
@@ -42,6 +43,45 @@ def delete_bucket(bucket_name):
42
43
print ('Bucket {} deleted' .format (bucket .name ))
43
44
44
45
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
+
45
85
def list_blobs (bucket_name ):
46
86
"""Lists all the blobs in the bucket."""
47
87
storage_client = storage .Client ()
@@ -222,6 +262,10 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
222
262
subparsers = parser .add_subparsers (dest = 'command' )
223
263
subparsers .add_parser ('create-bucket' , help = create_bucket .__doc__ )
224
264
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__ )
225
269
subparsers .add_parser ('list' , help = list_blobs .__doc__ )
226
270
227
271
list_with_prefix_parser = subparsers .add_parser (
@@ -268,6 +312,12 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
268
312
create_bucket (args .bucket_name )
269
313
elif args .command == 'delete-bucket' :
270
314
delete_bucket (args .bucket_name )
315
+ if args .command == 'get-bucket-labels' :
316
+ get_bucket_labels (a
8000
rgs .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 )
271
321
elif args .command == 'list' :
272
322
list_blobs (args .bucket_name )
273
323
elif args .command == 'list-with-prefix' :
0 commit comments