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: add sample for creating a bucket with hierarhical namespace … #2486

Merged
merged 6 commits into from
Jun 12, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <BUCKET_NAME>`


-----




### Create a Bucket with object retention enabled.

Create a Bucket with object retention enabled.
Expand Down
65 changes: 65 additions & 0 deletions samples/createBucketWithHierarchicalNamespace.js
Original file line number Diff line number Diff line change
@@ -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 <BUCKET_NAME>

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));
17 changes: 16 additions & 1 deletion samples/system-test/buckets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
Loading