diff --git a/samples/snippets/noxfile_config.py b/samples/snippets/noxfile_config.py index 4f184ede0..17a05b9f2 100644 --- a/samples/snippets/noxfile_config.py +++ b/samples/snippets/noxfile_config.py @@ -78,7 +78,7 @@ def get_cloud_kms_key(): TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. - 'ignored_versions': ["2.7", "3.6"], + 'ignored_versions': ["2.7", "3.6", "3.7", "3.11", "3.12"], # An envvar key for determining the project id to use. Change it # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a diff --git a/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index f9851b0ec..8c021f870 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/snippets_test.py @@ -37,6 +37,7 @@ import storage_cors_configuration import storage_create_bucket_class_location import storage_create_bucket_dual_region +import storage_create_bucket_hierarchical_namespace import storage_create_bucket_object_retention import storage_define_bucket_website_configuration import storage_delete_file @@ -841,3 +842,11 @@ def test_object_retention_policy(test_bucket_create, capsys): blob.retention.mode = None blob.retention.retain_until_time = None blob.patch(override_unlocked_retention=True) + + +def test_create_bucket_hierarchical_namespace(test_bucket_create, capsys): + storage_create_bucket_hierarchical_namespace.create_bucket_hierarchical_namespace( + test_bucket_create.name + ) + out, _ = capsys.readouterr() + assert f"Created bucket {test_bucket_create.name} with hierarchical namespace enabled" in out diff --git a/samples/snippets/storage_create_bucket_hierarchical_namespace.py b/samples/snippets/storage_create_bucket_hierarchical_namespace.py new file mode 100644 index 000000000..d9d310772 --- /dev/null +++ b/samples/snippets/storage_create_bucket_hierarchical_namespace.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +# Copyright 2024 Google LLC +# +# 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_create_bucket_hierarchical_namespace] +from google.cloud import storage + + +def create_bucket_hierarchical_namespace(bucket_name): + """Creates a bucket with hierarchical namespace enabled.""" + # The ID of your GCS bucket + # bucket_name = "your-bucket-name" + + storage_client = storage.Client() + bucket = storage_client.bucket(bucket_name) + bucket.iam_configuration.uniform_bucket_level_access_enabled = True + bucket.hierarchical_namespace_enabled = True + bucket.create() + + print(f"Created bucket {bucket_name} with hierarchical namespace enabled.") + + +# [END storage_create_bucket_hierarchical_namespace] + + +if __name__ == "__main__": + create_bucket_hierarchical_namespace(bucket_name=sys.argv[1])