Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: create bucket with HNS enabled #1285

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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])