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 6afeca5

Browse filesBrowse files
authored
storage: bucket policy only samples (GoogleCloudPlatform#1976)
* humble beginnings * Verified integration tests and updated README.rst * Updating samples to reflect fixed surface * Use release 1.14.0
1 parent 9df6c04 commit 6afeca5
Copy full SHA for 6afeca5

File tree

Expand file treeCollapse file tree

6 files changed

+192
-8
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+192
-8
lines changed

‎storage/cloud-client/README.rst

Copy file name to clipboardExpand all lines: storage/cloud-client/README.rst
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,39 @@ To run this sample:
300300
301301
302302
303+
Bucket Policy Only
304+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
305+
306+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
307+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/cloud-client/bucket_policy_only.py,storage/cloud-client/README.rst
308+
309+
310+
311+
312+
To run this sample:
313+
314+
.. code-block:: bash
315+
316+
$ python bucket_policy_only.py
317+
318+
usage: bucket_policy_only.py [-h]
319+
{enable-bucket-policy-only,disable-bucket-policy-only,get-bucket-policy-only}
320+
...
321+
322+
positional arguments:
323+
{enable-bucket-policy-only,disable-bucket-policy-only,get-bucket-policy-only}
324+
enable-bucket-policy-only
325+
Enable Bucket Policy Only for a bucket
326+
disable-bucket-policy-only
327+
Disable Bucket Policy Only for a bucket
328+
get-bucket-policy-only
329+
Get Bucket Policy Only for a bucket
330+
331+
optional arguments:
332+
-h, --help show this help message and exit
333+
334+
335+
303336
Notification Polling
304337
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
305338

‎storage/cloud-client/README.rst.in

Copy file name to clipboardExpand all lines: storage/cloud-client/README.rst.in
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ product:
44
name: Google Cloud Storage
55
short_name: Cloud Storage
66
url: https://cloud.google.com/storage/docs
7-
description: >
7+
description: >
88
`Google Cloud Storage`_ allows world-wide storage and retrieval of any
99
amount of data at any time.
1010

@@ -27,6 +27,9 @@ samples:
2727
- name: Bucket Lock
2828
file: bucket_lock.py
2929
show_help: true
30+
- name: Bucket Policy Only
31+
file: bucket_policy_only.py
32+
show_help: true
3033
- name: Notification Polling
3134
file: notification_polling.py
3235
show_help: true

‎storage/cloud-client/acl_test.py

Copy file name to clipboardExpand all lines: storage/cloud-client/acl_test.py
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def test_bucket():
3737
object_default_acl = google.cloud.storage.acl.DefaultObjectACL(bucket)
3838
acl.reload()
3939
object_default_acl.reload()
40-
time.sleep(1) # bucket ops rate limited 1 update per second
40+
time.sleep(1) # bucket ops rate limited 1 update per second
4141
yield bucket
42-
time.sleep(1) # bucket ops rate limited 1 update per second
42+
time.sleep(1) # bucket ops rate limited 1 update per second
4343
acl.save()
4444
object_default_acl.save()
4545

@@ -51,10 +51,10 @@ def test_blob():
5151
blob = bucket.blob('storage_acl_test_sigil')
5252
blob.upload_from_string('Hello, is it me you\'re looking for?')
5353
acl = google.cloud.storage.acl.ObjectACL(blob)
54-
acl.reload()
55-
time.sleep(1) # bucket ops rate limited 1 update per second
56-
yield blob
57-
time.sleep(1) # bucket ops rate limited 1 update per second
54+
acl.reload() # bucket ops rate limited 1 update per second
55+
time.sleep(1)
56+
yield blob # bucket ops rate limited 1 update per second
57+
time.sleep(1)
5858
acl.save()
5959

6060

+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google Inc. All Rights Reserved.
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+
import argparse
18+
19+
from google.cloud import storage
20+
21+
22+
def enable_bucket_policy_only(bucket_name):
23+
"""Enable Bucket Policy Only for a bucket"""
24+
# [START storage_enable_bucket_policy_only]
25+
# bucket_name = "my-bucket"
26+
27+
storage_client = storage.Client()
28+
bucket = storage_client.bucket(bucket_name)
29+
30+
bucket.iam_configuration.bucket_policy_only_enabled = True
31+
bucket.patch()
32+
33+
print('Bucket Policy Only was enabled for {}.'.format(bucket.name))
34+
# [END storage_enable_bucket_policy_only]
35+
36+
37+
def disable_bucket_policy_only(bucket_name):
38+
"""Disable Bucket Policy Only for a bucket"""
39+
# [START storage_disable_bucket_policy_only]
40+
# bucket_name = "my-bucket"
41+
42+
storage_client = storage.Client()
43+
bucket = storage_client.bucket(bucket_name)
44+
45+
bucket.iam_configuration.bucket_policy_only_enabled = False
46+
bucket.patch()
47+
48+
print('Bucket Policy Only was disabled for {}.'.format(bucket.name))
49+
# [END storage_disable_bucket_policy_only]
50+
51+
52+
def get_bucket_policy_only(bucket_name):
53+
"""Get Bucket Policy Only for a bucket"""
54+
# [START storage_get_bucket_policy_only]
55+
# bucket_name = "my-bucket"
56+
57+
storage_client = storage.Client()
58+
bucket = storage_client.get_bucket(bucket_name)
59+
iam_configuration = bucket.iam_configuration
60+
61+
if iam_configuration.bucket_policy_only_enabled:
62+
print('Bucket Policy Only is enabled for {}.'.format(bucket.name))
63+
print('Bucket will be locked on {}.'.format(
64+
iam_configuration.bucket_policy_only_locked_time))
65+
else:
66+
print('Bucket Policy Only is disabled for {}.'.format(bucket.name))
67+
# [END storage_get_bucket_policy_only]
68+
69+
70+
if __name__ == '__main__':
71+
72+
parser = argparse.ArgumentParser(
73+
description=__doc__,
74+
formatter_class=argparse.RawDescriptionHelpFormatter)
75+
subparsers = parser.add_subparsers(dest='command')
76+
77+
enable_bucket_policy_only_parser = subparsers.add_parser(
78+
'enable-bucket-policy-only', help=enable_bucket_policy_only.__doc__)
79+
enable_bucket_policy_only_parser.add_argument('bucket_name')
80+
81+
disable_bucket_policy_only_parser = subparsers.add_parser(
82+
'disable-bucket-policy-only', help=disable_bucket_policy_only.__doc__)
83+
disable_bucket_policy_only_parser.add_argument('bucket_name')
84+
85+
get_bucket_policy_only_parser = subparsers.add_parser(
86+
'get-bucket-policy-only', help=get_bucket_policy_only.__doc__)
87+
get_bucket_policy_only_parser.add_argument('bucket_name')
88+
89+
args = parser.parse_args()
90+
91+
if args.command == 'enable-bucket-policy-only':
92+
enable_bucket_policy_only(args.bucket_name)
93+
elif args.command == 'disable-bucket-policy-only':
94+
disable_bucket_policy_only(args.bucket_name)
95+
elif args.command == 'get-bucket-policy-only':
96+
get_bucket_policy_only(args.bucket_name)
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2019 Google Inc. All Rights Reserved.
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 time
16+
17+
from google.cloud import storage
18+
19+
import pytest
20+
21+
import bucket_policy_only
22+
23+
24+
@pytest.fixture()
25+
def bucket():
26+
"""Creates a test bucket and deletes it upon completion."""
27+
client = storage.Client()
28+
bucket_name = 'bucket-policy-only-' + str(int(time.time()))
29+
bucket = client.create_bucket(bucket_name)
30+
yield bucket
31+
bucket.delete(force=True)
32+
33+
34+
def test_get_bucket_policy_only(bucket, capsys):
35+
bucket_policy_only.get_bucket_policy_only(bucket.name)
36+
out, _ = capsys.readouterr()
37+
assert 'Bucket Policy Only is disabled for {}.'.format(
38+
bucket.name) in out
39+
40+
41+
def test_enable_bucket_policy_only(bucket, capsys):
42+
bucket_policy_only.enable_bucket_policy_only(bucket.name)
43+
out, _ = capsys.readouterr()
44+
assert 'Bucket Policy Only was enabled for {}.'.format(
45+
bucket.name) in out
46+
47+
48+
def test_disable_bucket_policy_only(bucket, capsys):
49+
bucket_policy_only.disable_bucket_policy_only(bucket.name)
50+
out, _ = capsys.readouterr()
51+
assert 'Bucket Policy Only was disabled for {}.'.format(
52+
bucket.name) in out

‎storage/cloud-client/requirements.txt

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
google-cloud-pubsub==0.39.1
2-
google-cloud-storage==1.13.2
2+
google-cloud-storage==1.14.0

0 commit comments

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