Skip to content

Commit 074261a

Browse files
authored
samples: create bucket with HNS enabled (#1285)
* samples: create bucket with HNS enabled * allow sample tests to run in specific runtimes
1 parent 3ce1a6b commit 074261a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

packages/google-cloud-storage/samples/snippets/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_cloud_kms_key():
7878

7979
TEST_CONFIG_OVERRIDE = {
8080
# You can opt out from the test for specific Python versions.
81-
'ignored_versions': ["2.7", "3.6"],
81+
'ignored_versions': ["2.7", "3.6", "3.7", "3.11", "3.12"],
8282

8383
# An envvar key for determining the project id to use. Change it
8484
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a

packages/google-cloud-storage/samples/snippets/snippets_test.py

Lines changed: 9 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_hierarchical_namespace
4041
import storage_create_bucket_object_retention
4142
import storage_define_bucket_website_configuration
4243
import storage_delete_file
@@ -841,3 +842,11 @@ def test_object_retention_policy(test_bucket_create, capsys):
841842
blob.retention.mode = None
842843
blob.retention.retain_until_time = None
843844
blob.patch(override_unlocked_retention=True)
845+
846+
847+
def test_create_bucket_hierarchical_namespace(test_bucket_create, capsys):
848+
storage_create_bucket_hierarchical_namespace.create_bucket_hierarchical_namespace(
849+
test_bucket_create.name
850+
)
851+
out, _ = capsys.readouterr()
852+
assert f"Created bucket {test_bucket_create.name} with hierarchical namespace enabled" in out
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_hierarchical_namespace]
20+
from google.cloud import storage
21+
22+
23+
def create_bucket_hierarchical_namespace(bucket_name):
24+
"""Creates a bucket with hierarchical namespace enabled."""
25+
# The ID of your GCS bucket
26+
# bucket_name = "your-bucket-name"
27+
28+
storage_client = storage.Client()
29+
bucket = storage_client.bucket(bucket_name)
30+
bucket.iam_configuration.uniform_bucket_level_access_enabled = True
31+
bucket.hierarchical_namespace_enabled = True
32+
bucket.create()
33+
34+
print(f"Created bucket {bucket_name} with hierarchical namespace enabled.")
35+
36+
37+
# [END storage_create_bucket_hierarchical_namespace]
38+
39+
40+
if __name__ == "__main__":
41+
create_bucket_hierarchical_namespace(bucket_name=sys.argv[1])

0 commit comments

Comments
 (0)