diff --git a/README.md b/README.md index c3b232821..f906c9f2d 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre | Generate V4 Read Signed Url | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4ReadSignedUrl.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/generateV4ReadSignedUrl.js,samples/README.md) | | Generate V4 Signed Policy | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4SignedPolicy.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/generateV4SignedPolicy.js,samples/README.md) | | Generate V4 Upload Signed Url | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/generateV4UploadSignedUrl.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/generateV4UploadSignedUrl.js,samples/README.md) | +| Get Autoclass | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getAutoclass.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/getAutoclass.js,samples/README.md) | | Get Default Event Based Hold | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getDefaultEventBasedHold.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/getDefaultEventBasedHold.js,samples/README.md) | | Get Metadata | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getMetadata.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/getMetadata.js,samples/README.md) | | Get Metadata Notifications | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getMetadataNotifications.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/getMetadataNotifications.js,samples/README.md) | @@ -190,6 +191,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre | Remove Retention Policy | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/removeRetentionPolicy.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/removeRetentionPolicy.js,samples/README.md) | | Rename File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/renameFile.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/renameFile.js,samples/README.md) | | Rotate Encryption Key | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/rotateEncryptionKey.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/rotateEncryptionKey.js,samples/README.md) | +| Set Autoclass | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setAutoclass.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/setAutoclass.js,samples/README.md) | | Set Client Endpoint | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setClientEndpoint.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/setClientEndpoint.js,samples/README.md) | | Set Event Based Hold | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setEventBasedHold.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/setEventBasedHold.js,samples/README.md) | | Set Public Access Prevention Enforced | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setPublicAccessPreventionEnforced.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/setPublicAccessPreventionEnforced.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 76d170303..c1cdd59b1 100644 --- a/samples/README.md +++ b/samples/README.md @@ -66,6 +66,7 @@ objects to users via direct download. * [Generate V4 Read Signed Url](#generate-v4-read-signed-url) * [Generate V4 Signed Policy](#generate-v4-signed-policy) * [Generate V4 Upload Signed Url](#generate-v4-upload-signed-url) + * [Get Autoclass](#get-autoclass) * [Get Default Event Based Hold](#get-default-event-based-hold) * [Get Metadata](#get-metadata) * [Get Metadata Notifications](#get-metadata-notifications) @@ -109,6 +110,7 @@ objects to users via direct download. * [Remove Retention Policy](#remove-retention-policy) * [Rename File](#rename-file) * [Rotate Encryption Key](#rotate-encryption-key) + * [Set Autoclass](#set-autoclass) * [Set Client Endpoint](#set-client-endpoint) * [Set Event Based Hold](#set-event-based-hold) * [Set Public Access Prevention Enforced](#set-public-access-prevention-enforced) @@ -991,6 +993,23 @@ __Usage:__ +### Get Autoclass + +View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getAutoclass.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/getAutoclass.js,samples/README.md) + +__Usage:__ + + +`node samples/getAutoclass.js` + + +----- + + + + ### Get Default Event Based Hold View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/getDefaultEventBasedHold.js). @@ -1746,6 +1765,23 @@ __Usage:__ +### Set Autoclass + +View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setAutoclass.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/setAutoclass.js,samples/README.md) + +__Usage:__ + + +`node samples/setAutoclass.js` + + +----- + + + + ### Set Client Endpoint View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setClientEndpoint.js). diff --git a/samples/getAutoclass.js b/samples/getAutoclass.js new file mode 100644 index 000000000..1bd33ae64 --- /dev/null +++ b/samples/getAutoclass.js @@ -0,0 +1,47 @@ +// Copyright 2022 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. + +/** + * This application demonstrates how to perform basic operations on files with + * the Google Cloud Storage API. + * + * For more information, see the README.md under /storage and the documentation + * at https://cloud.google.com/storage/docs. + */ + +function main(bucketName = 'my-bucket') { + // [START storage_get_autoclass] + /** + * 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 + const storage = new Storage(); + + async function getAutoclass() { + const [metadata] = await storage.bucket(bucketName).getMetadata(); + console.log( + `Autoclass enabled is set to ${metadata.autoclass.enabled} for ${metadata.name} at ${metadata.autoclass.toggleTime}.` + ); + } + + getAutoclass().catch(console.error); + // [END storage_get_autoclass] +} +main(...process.argv.slice(2)); diff --git a/samples/setAutoclass.js b/samples/setAutoclass.js new file mode 100644 index 000000000..6441f74b3 --- /dev/null +++ b/samples/setAutoclass.js @@ -0,0 +1,49 @@ +/** + * Copyright 2022 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. + */ + +function main(bucketName = 'my-bucket', toggle = false) { + // [START storage_set_autoclass] + /** + * 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 + const storage = new Storage(); + + async function setAutoclass() { + // Disables Autoclass for a bucket. + // Note: Only patch requests that disable autoclass are currently supported. + // To enable autoclass, you must set it at bucket creation time. + const [metadata] = await storage.bucket(bucketName).setMetadata({ + autoclass: { + enabled: toggle, + }, + }); + + console.log(`Autoclass enabled is set to ${metadata.autoclass.enabled} for + ${metadata.name} at ${metadata.autoclass.toggleTime}.`); + } + + setAutoclass().catch(console.error); + // [END storage_set_autoclass] +} + +main(...process.argv.slice(2)); diff --git a/samples/system-test/buckets.test.js b/samples/system-test/buckets.test.js index 9ecc516a5..f29c1657e 100644 --- a/samples/system-test/buckets.test.js +++ b/samples/system-test/buckets.test.js @@ -28,6 +28,7 @@ const bucketName = `${samplesTestBucketPrefix}-a`; const bucketNameDualRegion = `${samplesTestBucketPrefix}-b`; const bucketNameDualRegionTurbo = `${samplesTestBucketPrefix}-c`; const bucketNameWithClassAndLocation = `${samplesTestBucketPrefix}-d`; +const bucketNameAutoclass = `${samplesTestBucketPrefix}-e`; const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA; const bucket = storage.bucket(bucketName); const bucketWithClassAndLocation = storage.bucket( @@ -75,6 +76,23 @@ it('should get bucket metadata', async () => { assert.include(output, bucketName); }); +it('should disable autoclass', async () => { + await storage.createBucket(bucketNameAutoclass, { + autoclass: { + enabled: true, + }, + }); + const output = execSync( + `node setAutoclass.js ${bucketNameAutoclass} ${false}` + ); + assert.include(output, 'Autoclass'); +}); + +it('should get autoclass', async () => { + const output = execSync(`node getAutoclass.js ${bucketNameAutoclass}`); + assert.include(output, 'Autoclass enabled is set to false'); +}); + it('should set a buckets default KMS key', async () => { const output = execSync( `node enableDefaultKMSKey.js ${bucketName} ${defaultKmsKeyName}` diff --git a/src/storage.ts b/src/storage.ts index a40d1b1bb..b9065291b 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -107,8 +107,13 @@ export interface CustomPlacementConfig { dataLocations?: string[]; } +export interface AutoclassConfig { + enabled?: boolean; +} + export interface CreateBucketRequest { archive?: boolean; + autoclass?: AutoclassConfig; coldline?: boolean; cors?: Cors[]; customPlacementConfig?: CustomPlacementConfig; @@ -810,6 +815,8 @@ export class Storage extends Service { * * @typedef {object} CreateBucketRequest * @property {boolean} [archive=false] Specify the storage class as Archive. + * @property {object} [autoclass.enabled=false] Specify whether Autoclass is + * enabled for the bucket. * @property {boolean} [coldline=false] Specify the storage class as Coldline. * @property {Cors[]} [cors=[]] Specify the CORS configuration to use. * @property {CustomPlacementConfig} [customPlacementConfig={}] Specify the bucket's regions for dual-region buckets. diff --git a/system-test/storage.ts b/system-test/storage.ts index 37899a674..2e4489f32 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -1054,6 +1054,25 @@ describe('storage', () => { assert.strictEqual(metadata.storageClass, 'COLDLINE'); }); + it('should allow enabling & disabling autoclass', async () => { + const [bucket] = await storage.createBucket(generateName(), { + autoclass: { + enabled: true, + }, + }); + let [metadata] = await bucket.getMetadata(); + const timestampEnabled = metadata.autoclass.toggleTime; + assert.strictEqual(metadata.autoclass.enabled, true); + [metadata] = await bucket.setMetadata({ + autoclass: { + enabled: false, + }, + }); + const timestampDisabled = metadata.autoclass.toggleTime; + assert.strictEqual(metadata.autoclass.enabled, false); + assert.strictEqual(timestampDisabled > timestampEnabled, true); + }); + describe('locationType', () => { const types = ['multi-region', 'region', 'dual-region'];