8000 Add bucket label samples by theacodes · Pull Request #1045 · GoogleCloudPlatform/python-docs-samples · GitHub
[go: up one dir, main page]

Skip to content

Add bucket label samples #1045 8000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2017
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
50 changes: 50 additions & 0 deletions storage/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import argparse
import datetime
import pprint

from google.cloud import storage

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


def get_bucket_labels(bucket_name):
"""Prints out a bucket's labels."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
labels = bucket.labels
pprint.pprint(labels)


def add_bucket_label(bucket_name):
"""Add a label to a bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)

labels = bucket.labels
labels['example'] = 'label'
bucket.labels = labels
bucket.patch()

print('Updated labels on {}.'.format(bucket.name))
pprint.pprint(bucket.labels)


def remove_bucket_label(bucket_name):
"""Remove a label from a bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)

labels = bucket.labels

if 'example' in labels:
del labels['example']

bucket.labels = labels
bucket.patch()

print('Updated labels on {}.'.format(bucket.name))
pprint.pprint(bucket.labels)


def list_blobs(bucket_name):
"""Lists all the blobs in the bucket."""
storage_client = storage.Client()
Expand Down Expand Up @@ -222,6 +262,10 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('create-bucket', help=create_bucket.__doc__)
subparsers.add_parser('delete-bucket', help=delete_bucket.__doc__)
subparsers.add_parser('get-bucket-labels', help=get_bucket_labels.__doc__)
subparsers.add_parser('add-bucket-label', help=add_bucket_label.__doc__)
subparsers.add_parser(
'remove-bucket-label', help=remove_bucket_label.__doc__)
subparsers.add_parser('list', help=list_blobs.__doc__)

list_with_prefix_parser = subparsers.add_parser(
Expand Down Expand Up @@ -268,6 +312,12 @@ def copy_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
create_bucket(args.bucket_name)
elif args.command == 'delete-bucket':
delete_bucket(args.bucket_name)
if args.command == 'get-bucket-labels':
get_bucket_labels(args.bucket_name)
if args.command == 'add-bucket-label':
add_bucket_label(args.bucket_name)
if args.command == 'remove-bucket-label':
remove_bucket_label(args.bucket_name)
elif args.command == 'list':
list_blobs(args.bucket_name)
elif args.command == 'list-with-prefix':
Expand Down
21 changes: 21 additions & 0 deletions storage/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']


def test_get_bucket_labels():
snippets.get_bucket_labels(BUCKET)


def test_add_bucket_label(capsys):
snippets.add_bucket_label(BUCKET)
out, _ = capsys.readouterr()
assert 'example' in out


@pytest.mark.xfail(
reason=(
'https://github.com/GoogleCloudPlatform'
'/google-cloud-python/issues/3711'))
def test_remove_bucket_label(capsys):
snippets.add_bucket_label(BUCKET)
snippets.remove_bucket_label(BUCKET)
out, _ = capsys.readouterr()
assert '{}' in out


@pytest.fixture
def test_blob():
"""Provides a pre-existing blob in the test bucket."""
Expand Down
0