8000 storage requester pays samples (#1122) · johnmanong/python-docs-samples@9113f75 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9113f75

Browse files
ryanmatsJon Wayne Parrott
authored andcommitted
storage requester pays samples (GoogleCloudPlatform#1122)
* storage requester pays samples * Added tests and fixed linting issues * google-cloud-storage version update * changed get_bucket to bucket for downloading * small change
1 parent b3f046c commit 9113f75

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to use requester pays features on Google
18+
Cloud Storage buckets.
19+
20+
For more information, see the documentation at
21+
https://cloud.google.com/storage/docs/using-requester-pays.
22+
"""
23+
24+
import argparse
25+
26+
from google.cloud import storage
27+
28+
29+
def get_requester_pays_status(bucket_name):
30+
"""Get a bucket's requester pays metadata"""
31+
storage_client = storage.Client()
32+
bucket = storage_client.get_bucket(bucket_name)
33+
requester_pays_status = bucket.requester_pays
34+
if requester_pays_status:
35+
print('Requester Pays is enabled for {}'.format(bucket_name))
36+
else:
37+
print('Requester Pays is disabled for {}'.format(bucket_name))
38+
39+
40+
def enable_requester_pays(bucket_name):
41+
"""Enable a bucket's requesterpays metadata"""
42+
storage_client = storage.Client()
43+
bucket = storage_client.get_bucket(bucket_name)
44+
bucket.requester_pays = True
45+
bucket.patch()
46+
print('Requester Pays has been enabled for {}'.format(bucket_name))
47+
48+
49+
def disable_requester_pays(bucket_name):
50+
"""Disable a bucket's requesterpays metadata"""
51+
storage_client = storage.Client()
52+
bucket = storage_client.get_bucket(bucket_name)
53+
bucket.requester_pays = False
54+
bucket.patch()
55+
print('Requester Pays has been disabled for {}'.format(bucket_name))
56+
57+
58+
def download_file_requester_pays(
59+
bucket_name, project_id, source_blob_name, destination_file_name):
60+
"""Download file using specified project as the requester"""
61+
storage_client = storage.Client()
62+
user_project = project_id
63+
bucket = storage_client.bucket(bucket_name, user_project)
64+
blob = bucket.blob(source_blob_name)
65+
blob.download_to_filename(destination_file_name)
66+
67+
print('Blob {} downloaded to {} using a requester-pays request.'.format(
68+
source_blob_name,
69+
destination_file_name))
70+
71+
72+
if __name__ == '__main__':
73+
parser = argparse.ArgumentParser(
74+
description=__doc__,
75+
formatter_class=argparse.RawDescriptionHelpFormatter)
76+
parser.add_argument('bucket_name', help='Your Cloud Storage bucket name.')
77+
subparsers = parser.add_subparsers(dest='command')
78+
79+
subparsers.add_parser(
80+
'check-status', help=get_requester_pays_status.__doc__)
81+
82+
subparsers.add_parser(
83+
'enable', help=enable_requester_pays.__doc__)
84+
85+
subparsers.add_parser(
86+
'disable', help=disable_requester_pays.__doc__)
87+
88+
download_parser = subparsers.add_parser(
89+
'download', help=download_file_requester_pays.__doc__)
90+
download_parser.add_argument('project')
91+
download_parser.add_argument('source_blob_name')
92+
download_parser.add_argument('destination_file_name')
93+
94+
args = parser.parse_args()
95+
96+
if args.command == 'check-status':
97+
get_requester_pays_status(args.bucket_name)
98+
elif args.command == 'enable':
99+
enable_requester_pays(args.bucket_name)
100+
elif args.command == 'disable':
101+
disable_requester_pays(args.bucket_name)
102+
elif args.command == 'download':
103+
download_file_requester_pays(
104+
args.bucket_name, args.project, args.source_blob_name,
105+
args.destination_file_name)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2017 Google, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import tempfile
17+
18+
from google.cloud import storage
19+
import pytest
20+
21+
import requester_pays
22+
23+
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
24+
PROJECT = os.environ['GCLOUD_PROJECT']
25+
26+
27+
def test_enable_requester_pays(capsys):
28+
requester_pays.enable_requester_pays(BUCKET)
29+
out, _ = capsys.readouterr()
30+
assert 'Requester Pays has been enabled for {}'.format(BUCKET) in out
31+
32+
33+
def test_disable_requester_pays(capsys):
34+
requester_pays.disable_requester_pays(BUCKET)
35+
out, _ = capsys.readouterr()
36+
assert 'Requester Pays has been disabled for {}'.format(BUCKET) in out
37+
38+
39+
def test_get_requester_pays_status(capsys):
40+
requester_pays.get_requester_pays_status(BUCKET)
41+
out, _ = capsys.readouterr()
42+
assert 'Requester Pays is disabled for {}'.format(BUCKET) in out
43+
44+
45+
@pytest.fixture
46+
def test_blob():
47+
"""Provides a pre-existing blob in the test bucket."""
48+
bucket = storage.Client().bucket(BUCKET)
49+
blob = bucket.blob('storage_snippets_test_sigil')
50+
blob.upload_from_string('Hello, is it me you\'re looking for?')
51+
return blob
52+
53+
54+
def test_download_file_requester_pays(test_blob, capsys):
55+
with tempfile.NamedTemporaryFile() as dest_file:
56+
requester_pays.download_file_requester_pays(
57+
BUCKET,
58+
PROJECT,
59+
test_blob.name,
60+
dest_file.name)
61+
62+
assert dest_file.read()

0 commit comments

Comments
 (0)
0