Skip to content

Commit 343f4c0

Browse files
authored
samples: add samples for object retention (#1247)
1 parent bdd426a commit 343f4c0

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

samples/snippets/snippets_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import storage_cors_configuration
3838
import storage_create_bucket_class_location
3939
import storage_create_bucket_dual_region
40+
import storage_create_bucket_object_retention
4041
import storage_define_bucket_website_configuration
4142
import storage_delete_file
4243
import storage_delete_file_archived_generation
@@ -71,6 +72,7 @@
7172
import storage_set_autoclass
7273
import storage_set_bucket_default_kms_key
7374
import storage_set_client_endpoint
75+
import storage_set_object_retention_policy
7476
import storage_set_metadata
7577
import storage_transfer_manager_download_bucket
7678
import storage_transfer_manager_download_chunks_concurrently
@@ -818,3 +820,24 @@ def test_transfer_manager_upload_chunks_concurrently(test_bucket, capsys):
818820

819821
out, _ = capsys.readouterr()
820822
assert "File {} uploaded to {}".format(file.name, BLOB_NAME) in out
823+
824+
825+
def test_object_retention_policy(test_bucket_create, capsys):
826+
storage_create_bucket_object_retention.create_bucket_object_retention(
827+
test_bucket_create.name
828+
)
829+
out, _ = capsys.readouterr()
830+
assert f"Created bucket {test_bucket_create.name} with object retention enabled setting" in out
831+
832+
blob_name = "test_object_retention"
833+
storage_set_object_retention_policy.set_object_retention_policy(
834+
test_bucket_create.name, "hello world", blob_name
835+
)
836+
out, _ = capsys.readouterr()
837+
assert f"Retention policy for file {blob_name}" in out
838+
839+
# Remove retention policy for test cleanup
840+
blob = test_bucket_create.blob(blob_name)
841+
blob.retention.mode = None
842+
blob.retention.retain_until_time = None
843+
blob.patch(override_unlocked_retention=True)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
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_create_bucket_with_object_retention]
20+
from google.cloud import storage
21+
22+
23+
def create_bucket_object_retention(bucket_name):
24+
"""Creates a bucket with object retention enabled."""
25+
# The ID of your GCS bucket
26+
# bucket_name = "your-bucket-name"
27+
28+
storage_client = storage.Client()
29+
bucket = storage_client.create_bucket(bucket_name, enable_object_retention=True)
30+
31+
print(f"Created bucket {bucket_name} with object retention enabled setting: {bucket.object_retention_mode}")
32+
33+
34+
# [END storage_create_bucket_with_object_retention]
35+
36+
37+
if __name__ == "__main__":
38+
create_bucket_object_retention(bucket_name=sys.argv[1])

samples/snippets/storage_get_bucket_metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def bucket_metadata(bucket_name):
4444
print(f"Retention Effective Time: {bucket.retention_policy_effective_time}")
4545
print(f"Retention Period: {bucket.retention_period}")
4646
print(f"Retention Policy Locked: {bucket.retention_policy_locked}")
47+
print(f"Object Retention Mode: {bucket.object_retention_mode}")
4748
print(f"Requester Pays: {bucket.requester_pays}")
4849
print(f"Self Link: {bucket.self_link}")
4950
print(f"Time Created: {bucket.time_created}")

samples/snippets/storage_get_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def blob_metadata(bucket_name, blob_name):
5959
"Event based hold: ",
6060
"enabled" if blob.event_based_hold else "disabled",
6161
)
62+
print(f"Retention mode: {blob.retention.mode}")
63+
print(f"Retention retain until time: {blob.retention.retain_until_time}")
6264
if blob.retention_expiration_time:
6365
print(
6466
f"retentionExpirationTime: {blob.retention_expiration_time}"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
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 datetime
18+
import sys
19+
20+
# [START storage_set_object_retention_policy]
21+
from google.cloud import storage
22+
23+
24+
def set_object_retention_policy(bucket_name, contents, destination_blob_name):
25+
"""Set the object retention policy of a file."""
26+
27+
# The ID of your GCS bucket
28+
# bucket_name = "your-bucket-name"
29+
30+
# The contents to upload to the file
31+
# contents = "these are my contents"
32+
33+
# The ID of your GCS object
34+
# destination_blob_name = "storage-object-name"
35+
36+
storage_client = storage.Client()
37+
bucket = storage_client.bucket(bucket_name)
38+
blob = bucket.blob(destination_blob_name)
39+
blob.upload_from_string(contents)
40+
41+
# Set the retention policy for the file.
42+
blob.retention.mode = "Unlocked"
43+
retention_date = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=10)
44+
blob.retention.retain_until_time = retention_date
45+
blob.patch()
46+
print(
47+
f"Retention policy for file {destination_blob_name} was set to: {blob.retention.mode}."
48+
)
49+
50+
# To modify an existing policy on an unlocked file object, pass in the override parameter.
51+
new_retention_date = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=9)
52+
blob.retention.retain_until_time = new_retention_date
53+
blob.patch(override_unlocked_retention=True)
54+
print(
55+
f"Retention policy for file {destination_blob_name} was updated to: {blob.retention.retain_until_time}."
56+
)
57+
58+
59+
# [END storage_set_object_retention_policy]
60+
61+
62+
if __name__ == "__main__":
63+
set_object_retention_policy(
64+
bucket_name=sys.argv[1],
65+
contents=sys.argv[2],
66+
destination_blob_name=sys.argv[3],
67+
)

0 commit comments

Comments
 (0)