Skip to content

Commit 4d4147d

Browse files
docs(storage): add samples for lifer cycle and versioning (#3578)
* docs(storage): add samples for lifer cycle and versioning * docs(storage): nits * docs(storage): lint fix Co-authored-by: Leah E. Cole <[email protected]>
1 parent af91571 commit 4d4147d

6 files changed

+244
-0
lines changed

storage/cloud-client/snippets_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import storage_get_bucket_labels
3030
import storage_get_bucket_metadata
3131
import storage_get_metadata
32+
import storage_set_metadata
3233
import storage_list_buckets
3334
import storage_list_files_with_prefix
3435
import storage_list_files
@@ -42,6 +43,10 @@
4243
import storage_generate_upload_signed_url_v4
4344
import storage_generate_signed_post_policy_v4
4445
import storage_set_bucket_default_kms_key
46+
import storage_enable_versioning
47+
import storage_disable_versioning
48+
import storage_enable_bucket_lifecycle_management
49+
import storage_disable_bucket_lifecycle_management
4550

4651
KMS_KEY = os.environ["CLOUD_KMS_KEY"]
4752

@@ -156,6 +161,12 @@ def test_blob_metadata(test_blob, capsys):
156161
assert test_blob.name in out
157162

158163

164+
def test_set_blob_metadata(test_blob, capsys):
165+
storage_set_metadata.set_blob_metadata(test_blob.bucket.name, test_blob.name)
166+
out, _ = capsys.readouterr()
167+
assert test_blob.name in out
168+
169+
159170
def test_delete_blob(test_blob):
160171
storage_delete_file.delete_blob(test_blob.bucket.name, test_blob.name)
161172

@@ -249,3 +260,30 @@ def test_copy_blob(test_blob):
249260

250261
assert bucket.get_blob("test_copy_blob") is not None
251262
assert bucket.get_blob(test_blob.name) is not None
263+
264+
265+
def test_versioning(test_bucket, capsys):
266+
bucket = storage_enable_versioning.enable_versioning(test_bucket)
267+
out, _ = capsys.readouterr()
268+
assert "Versioning was enabled for bucket" in out
269+
assert bucket.versioning_enabled is True
270+
271+
bucket = storage_disable_versioning.disable_versioning(test_bucket)
272+
out, _ = capsys.readouterr()
273+
assert "Versioning was disabled for bucket" in out
274+
assert bucket.versioning_enabled is False
275+
276+
277+
def test_bucket_lifecycle_management(test_bucket, capsys):
278+
bucket = storage_enable_bucket_lifecycle_management.\
279+
enable_bucket_lifecycle_management(test_bucket)
280+
out, _ = capsys.readouterr()
281+
assert "[]" in out
282+
assert "Lifecycle management is enable" in out
283+
assert len(list(bucket.lifecycle_rules)) > 0
284+
285+
bucket = storage_disable_bucket_lifecycle_management.\
286+
disable_bucket_lifecycle_management(test_bucket)
287+
out, _ = capsys.readouterr()
288+
assert "[]" in out
289+
assert len(list(bucket.lifecycle_rules)) == 0
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 sys
18+
19+
# [START storage_disable_bucket_lifecycle_management]
20+
from google.cloud import storage
21+
22+
23+
def disable_bucket_lifecycle_management(bucket_name):
24+
"""Disable lifecycle management for a bucket"""
25+
# bucket_name = "my-bucket"
26+
27+
storage_client = storage.Client()
28+
29+
bucket = storage_client.get_bucket(bucket_name)
30+
bucket.clear_lifecyle_rules()
31+
bucket.patch()
32+
rules = bucket.lifecycle_rules
33+
34+
print("Lifecycle management is disable for bucket {} and the rules are {}".format(bucket_name, list(rules)))
35+
return bucket
36+
37+
38+
# [END storage_disable_bucket_lifecycle_management]
39+
40+
if __name__ == "__main__":
41+
disable_bucket_lifecycle_management(bucket_name=sys.argv[1])
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 sys
18+
19+
# [START storage_disable_versioning]
20+
from google.cloud import storage
21+
22+
23+
def disable_versioning(bucket_name):
24+
"""Disable versioning for this bucket."""
25+
# bucket_name = "my-bucket"
26+
27+
storage_client = storage.Client()
28+
29+
bucket = storage_client.get_bucket(bucket_name)
30+
bucket.versioning_enabled = False
31+
bucket.patch()
32+
33+
print("Versioning was disabled for bucket {}".format(bucket))
34+
return bucket
35+
36+
37+
# [END storage_enable_versioning]
38+
39+
if __name__ == "__main__":
40+
disable_versioning(bucket_name=sys.argv[1])
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 sys
18+
19+
# [START storage_enable_bucket_lifecycle_management]
20+
from google.cloud import storage
21+
22+
23+
def enable_bucket_lifecycle_management(bucket_name):
24+
"""Enable lifecycle management for a bucket"""
25+
# bucket_name = "my-bucket"
26+
27+
storage_client = storage.Client()
28+
29+
bucket = storage_client.get_bucket(bucket_name)
30+
rules = bucket.lifecycle_rules
31+
32+
print("Lifecycle management rules for bucket {} are {}".format(bucket_name, list(rules)))
33+
bucket.add_lifecycle_delete_rule(age=2)
34+
bucket.patch()
35+
36+
rules = bucket.lifecycle_rules
37+
print("Lifecycle management is enable for bucket {} and the rules are {}".format(bucket_name, list(rules)))
38+
39+
return bucket
40+
41+
42+
# [END storage_enable_bucket_lifecycle_management]
43+
44+
if __name__ == "__main__":
45+
enable_bucket_lifecycle_management(bucket_name=sys.argv[1])
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 sys
18+
19+
# [START storage_enable_versioning]
20+
from google.cloud import storage
21+
22+
23+
def enable_versioning(bucket_name):
24+
"""Enable versioning for this bucket."""
25+
# bucket_name = "my-bucket"
26+
27+
storage_client = storage.Client()
28+
29+
bucket = storage_client.get_bucket(bucket_name)
30+
bucket.versioning_enabled = True
31+
bucket.patch()
32+
33+
print("Versioning was enabled for bucket {}".format(bucket.name))
34+
return bucket
35+
36+
37+
# [END storage_enable_versioning]
38+
39+
if __name__ == "__main__":
40+
enable_versioning(bucket_name=sys.argv[1])
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 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 sys
18+
19+
# [START storage_set_metadata]
20+
from google.cloud import storage
21+
22+
23+
def set_blob_metadata(bucket_name, blob_name):
24+
"""Set a blob's metadata."""
25+
# bucket_name = 'your-bucket-name'
26+
# blob_name = 'your-object-name'
27+
28+
storage_client = storage.Client()
29+
bucket = storage_client.bucket(bucket_name)
30+
blob = bucket.get_blob(blob_name)
31+
metadata = {'color': 'Red', 'name': 'Test'}
32+
blob.metadata = metadata
33+
34+
print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))
35+
36+
37+
# [END storage_get_metadata]
38+
39+
if __name__ == "__main__":
40+
set_blob_metadata(bucket_name=sys.argv[1], blob_name=sys.argv[2])

0 commit comments

Comments
 (0)