-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <6719667+leahecole@users.noreply.github.com>
- Loading branch information
1 parent
af91571
commit 4d4147d
Showing
6 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
storage/cloud-client/storage_disable_bucket_lifecycle_management.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
storage/cloud-client/storage_enable_bucket_lifecycle_management.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |