Skip to content

Commit

Permalink
docs(storage): add samples for lifer cycle and versioning (#3578)
Browse files Browse the repository at this point in the history
* docs(storage): add samples for lifer cycle and versioning

* docs(storage): nits

* docs(storage): lint fix

Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
  • Loading branch information
HemangChothani and leahecole authored May 4, 2020
1 parent af91571 commit 4d4147d
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 0 deletions.
38 changes: 38 additions & 0 deletions storage/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import storage_get_bucket_labels
import storage_get_bucket_metadata
import storage_get_metadata
import storage_set_metadata
import storage_list_buckets
import storage_list_files_with_prefix
import storage_list_files
Expand All @@ -42,6 +43,10 @@
import storage_generate_upload_signed_url_v4
import storage_generate_signed_post_policy_v4
import storage_set_bucket_default_kms_key
import storage_enable_versioning
import storage_disable_versioning
import storage_enable_bucket_lifecycle_management
import storage_disable_bucket_lifecycle_management

KMS_KEY = os.environ["CLOUD_KMS_KEY"]

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


def test_set_blob_metadata(test_blob, capsys):
storage_set_metadata.set_blob_metadata(test_blob.bucket.name, test_blob.name)
out, _ = capsys.readouterr()
assert test_blob.name in out


def test_delete_blob(test_blob):
storage_delete_file.delete_blob(test_blob.bucket.name, test_blob.name)

Expand Down Expand Up @@ -249,3 +260,30 @@ def test_copy_blob(test_blob):

assert bucket.get_blob("test_copy_blob") is not None
assert bucket.get_blob(test_blob.name) is not None


def test_versioning(test_bucket, capsys):
bucket = storage_enable_versioning.enable_versioning(test_bucket)
out, _ = capsys.readouterr()
assert "Versioning was enabled for bucket" in out
assert bucket.versioning_enabled is True

bucket = storage_disable_versioning.disable_versioning(test_bucket)
out, _ = capsys.readouterr()
assert "Versioning was disabled for bucket" in out
assert bucket.versioning_enabled is False


def test_bucket_lifecycle_management(test_bucket, capsys):
bucket = storage_enable_bucket_lifecycle_management.\
enable_bucket_lifecycle_management(test_bucket)
out, _ = capsys.readouterr()
assert "[]" in out
assert "Lifecycle management is enable" in out
assert len(list(bucket.lifecycle_rules)) > 0

bucket = storage_disable_bucket_lifecycle_management.\
disable_bucket_lifecycle_management(test_bucket)
out, _ = capsys.readouterr()
assert "[]" in out
assert len(list(bucket.lifecycle_rules)) == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_disable_bucket_lifecycle_management]
from google.cloud import storage


def disable_bucket_lifecycle_management(bucket_name):
"""Disable lifecycle management for a bucket"""
# bucket_name = "my-bucket"

storage_client = storage.Client()

bucket = storage_client.get_bucket(bucket_name)
bucket.clear_lifecyle_rules()
bucket.patch()
rules = bucket.lifecycle_rules

print("Lifecycle management is disable for bucket {} and the rules are {}".format(bucket_name, list(rules)))
return bucket


# [END storage_disable_bucket_lifecycle_management]

if __name__ == "__main__":
disable_bucket_lifecycle_management(bucket_name=sys.argv[1])
40 changes: 40 additions & 0 deletions storage/cloud-client/storage_disable_versioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_disable_versioning]
from google.cloud import storage


def disable_versioning(bucket_name):
"""Disable versioning for this bucket."""
# bucket_name = "my-bucket"

storage_client = storage.Client()

bucket = storage_client.get_bucket(bucket_name)
bucket.versioning_enabled = False
bucket.patch()

print("Versioning was disabled for bucket {}".format(bucket))
return bucket


# [END storage_enable_versioning]

if __name__ == "__main__":
disable_versioning(bucket_name=sys.argv[1])
45 changes: 45 additions & 0 deletions storage/cloud-client/storage_enable_bucket_lifecycle_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_enable_bucket_lifecycle_management]
from google.cloud import storage


def enable_bucket_lifecycle_management(bucket_name):
"""Enable lifecycle management for a bucket"""
# bucket_name = "my-bucket"

storage_client = storage.Client()

bucket = storage_client.get_bucket(bucket_name)
rules = bucket.lifecycle_rules

print("Lifecycle management rules for bucket {} are {}".format(bucket_name, list(rules)))
bucket.add_lifecycle_delete_rule(age=2)
bucket.patch()

rules = bucket.lifecycle_rules
print("Lifecycle management is enable for bucket {} and the rules are {}".format(bucket_name, list(rules)))

return bucket


# [END storage_enable_bucket_lifecycle_management]

if __name__ == "__main__":
enable_bucket_lifecycle_management(bucket_name=sys.argv[1])
40 changes: 40 additions & 0 deletions storage/cloud-client/storage_enable_versioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_enable_versioning]
from google.cloud import storage


def enable_versioning(bucket_name):
"""Enable versioning for this bucket."""
# bucket_name = "my-bucket"

storage_client = storage.Client()

bucket = storage_client.get_bucket(bucket_name)
bucket.versioning_enabled = True
bucket.patch()

print("Versioning was enabled for bucket {}".format(bucket.name))
return bucket


# [END storage_enable_versioning]

if __name__ == "__main__":
enable_versioning(bucket_name=sys.argv[1])
40 changes: 40 additions & 0 deletions storage/cloud-client/storage_set_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_set_metadata]
from google.cloud import storage


def set_blob_metadata(bucket_name, blob_name):
"""Set a blob's metadata."""
# bucket_name = 'your-bucket-name'
# blob_name = 'your-object-name'

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.get_blob(blob_name)
metadata = {'color': 'Red', 'name': 'Test'}
blob.metadata = metadata

print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))


# [END storage_get_metadata]

if __name__ == "__main__":
set_blob_metadata(bucket_name=sys.argv[1], blob_name=sys.argv[2])

0 comments on commit 4d4147d

Please sign in to comment.