diff --git a/README.md b/README.md index 090440ab4..8480241f1 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre | Copy File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/copyFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/copyFile.js,samples/README.md) | | Copy Old Version Of File. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/copyOldVersionOfFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/copyOldVersionOfFile.js,samples/README.md) | | Create a Dual-Region Bucket | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithDualRegion.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithDualRegion.js,samples/README.md) | +| Create a hierarchical namespace enabled bucket | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithHierarchicalNamespace.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithHierarchicalNamespace.js,samples/README.md) | | Create a Bucket with object retention enabled. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithObjectRetention.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithObjectRetention.js,samples/README.md) | | Create Bucket With Storage Class and Location. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithStorageClassAndLocation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithStorageClassAndLocation.js,samples/README.md) | | Create Bucket With Turbo Replication | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithTurboReplication.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithTurboReplication.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index e6371fe95..6a748e19c 100644 --- a/samples/README.md +++ b/samples/README.md @@ -34,6 +34,7 @@ objects to users via direct download. * [Copy File](#copy-file) * [Copy Old Version Of File.](#copy-old-version-of-file.) * [Create a Dual-Region Bucket](#create-a-dual-region-bucket) + * [Create a hierarchical namespace enabled bucket](#create-a-hierarchical-namespace-enabled-bucket) * [Create a Bucket with object retention enabled.](#create-a-bucket-with-object-retention-enabled.) * [Create Bucket With Storage Class and Location.](#create-bucket-with-storage-class-and-location.) * [Create Bucket With Turbo Replication](#create-bucket-with-turbo-replication) @@ -443,6 +444,25 @@ __Usage:__ +### Create a hierarchical namespace enabled bucket + +Create a hierarchical namespace enabled bucket. + +View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithHierarchicalNamespace.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/createBucketWithHierarchicalNamespace.js,samples/README.md) + +__Usage:__ + + +`node createBucketWithHierarchicalNamespace.js ` + + +----- + + + + ### Create a Bucket with object retention enabled. Create a Bucket with object retention enabled. diff --git a/samples/createBucketWithHierarchicalNamespace.js b/samples/createBucketWithHierarchicalNamespace.js new file mode 100644 index 000000000..8641cacff --- /dev/null +++ b/samples/createBucketWithHierarchicalNamespace.js @@ -0,0 +1,65 @@ +/** + * 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. + */ + +'use strict'; + +// sample-metadata: +// title: Create a hierarchical namespace enabled bucket +// description: Create a hierarchical namespace enabled bucket. +// usage: node createBucketWithHierarchicalNamespace.js + +function main(bucketName = 'my-bucket') { + // [START storage_create_bucket_hierarchical_namespace] + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // The ID of your GCS bucket + // const bucketName = 'your-unique-bucket-name'; + + // Imports the Google Cloud client library + const {Storage} = require('@google-cloud/storage'); + + // Creates a client + // The bucket in the sample below will be created in the project associated with this client. + // For more information, please see https://cloud.google.com/docs/authentication/production or https://googleapis.dev/nodejs/storage/latest/Storage.html + const storage = new Storage(); + + async function createBucketWithHierarchicalNamespace() { + const [bucket] = await storage.createBucket(bucketName, { + iamConfiguration: { + uniformBucketLevelAccess: { + enabled: true, + }, + }, + hierarchicalNamespace: { + enabled: true, + }, + }); + + console.log( + `Created '${bucket.name}' with hierarchical namespace enabled.` + ); + } + + createBucketWithHierarchicalNamespace().catch(console.error); + // [END storage_create_bucket_hierarchical_namespace] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/samples/system-test/buckets.test.js b/samples/system-test/buckets.test.js index bf2ee08fe..01daaa28e 100644 --- a/samples/system-test/buckets.test.js +++ b/samples/system-test/buckets.test.js @@ -30,6 +30,7 @@ const bucketNameDualRegionTurbo = `${samplesTestBucketPrefix}-c`; const bucketNameWithClassAndLocation = `${samplesTestBucketPrefix}-d`; const bucketNameAutoclass = `${samplesTestBucketPrefix}-e`; const bucketNameObjectRetention = `${samplesTestBucketPrefix}-f`; +const bucketNameHierarchicalNamespace = `${samplesTestBucketPrefix}-g`; const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA; const bucket = storage.bucket(bucketName); const bucketWithClassAndLocation = storage.bucket( @@ -333,6 +334,21 @@ it("should set a bucket's RPO to DEFAULT", async () => { assert.strictEqual(metadata[0].rpo, RPO_DEFAULT); }); +it('should create a hierarchical namespace enabled bucket', async () => { + const output = execSync( + `node createBucketWithHierarchicalNamespace.js ${bucketNameHierarchicalNamespace}` + ); + assert.match( + output, + new RegExp( + `Created '${bucketNameHierarchicalNamespace}' with hierarchical namespace enabled.` + ) + ); + + const metadata = await dualRegionBucketTurbo.getMetadata(); + assert.strictEqual(metadata[0].rpo, RPO_DEFAULT); +}); + it("should add a bucket's website configuration", async () => { const output = execSync( `node addBucketWebsiteConfiguration.js ${bucketName} http://example.com http://example.com/404.html` @@ -427,7 +443,6 @@ it('should create a bucket with object retention enabled', async () => { const output = execSync( `node createBucketWithObjectRetention.js ${bucketNameObjectRetention}` ); - console.log(output); assert.include( output, `Created '${bucketNameObjectRetention}' with object retention enabled setting: Enabled`