Skip to content

Commit

Permalink
samples: create bucket with HNS enabled (#1285)
Browse files Browse the repository at this point in the history
* samples: create bucket with HNS enabled

* allow sample tests to run in specific runtimes
  • Loading branch information
cojenco authored Jun 11, 2024
1 parent 64edbd9 commit 693f195
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion samples/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
41 changes: 41 additions & 0 deletions samples/snippets/storage_create_bucket_hierarchical_namespace.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 693f195

Please sign in to comment.