Skip to content

Commit 261d119

Browse files
samples: add samples for object retention (#2428)
* samples: add samples for object retention * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 36d2721 commit 261d119

File tree

7 files changed

+227
-1
lines changed

7 files changed

+227
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
115115
| 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) |
116116
| 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) |
117117
| 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) |
118+
| 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) |
118119
| 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) |
119120
| 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) |
120121
| Create New Bucket | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createNewBucket.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/createNewBucket.js,samples/README.md) |
@@ -197,6 +198,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
197198
| 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) |
198199
| 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) |
199200
| 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) |
201+
| Set the object retention policy of a File. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setObjectRetentionPolicy.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/setObjectRetentionPolicy.js,samples/README.md) |
200202
| 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) |
201203
| Set Public Access Prevention Inherited | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setPublicAccessPreventionInherited.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/setPublicAccessPreventionInherited.js,samples/README.md) |
202204
| Set RPO Async Turbo | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setRPOAsyncTurbo.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/setRPOAsyncTurbo.js,samples/README.md) |

samples/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ objects to users via direct download.
3434
* [Copy File](#copy-file)
3535
* [Copy Old Version Of File.](#copy-old-version-of-file.)
3636
* [Create a Dual-Region Bucket](#create-a-dual-region-bucket)
37+
* [Create a Bucket with object retention enabled.](#create-a-bucket-with-object-retention-enabled.)
3738
* [Create Bucket With Storage Class and Location.](#create-bucket-with-storage-class-and-location.)
3839
* [Create Bucket With Turbo Replication](#create-bucket-with-turbo-replication)
3940
* [Create New Bucket](#create-new-bucket)
@@ -116,6 +117,7 @@ objects to users via direct download.
116117
* [Set Autoclass](#set-autoclass)
117118
* [Set Client Endpoint](#set-client-endpoint)
118119
* [Set Event Based Hold](#set-event-based-hold)
120+
* [Set the object retention policy of a File.](#set-the-object-retention-policy-of-a-file.)
119121
* [Set Public Access Prevention Enforced](#set-public-access-prevention-enforced)
120122
* [Set Public Access Prevention Inherited](#set-public-access-prevention-inherited)
121123
* [Set RPO Async Turbo](#set-rpo-async-turbo)
@@ -441,6 +443,25 @@ __Usage:__
441443

442444

443445

446+
### Create a Bucket with object retention enabled.
447+
448+
Create a Bucket with object retention enabled.
449+
450+
View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/createBucketWithObjectRetention.js).
451+
452+
[![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)
453+
454+
__Usage:__
455+
456+
457+
`node createBucketWithObjectRetention.js <BUCKET_NAME>`
458+
459+
460+
-----
461+
462+
463+
464+
444465
### Create Bucket With Storage Class and Location.
445466

446467
Create Bucket With Storage Class and Location.
@@ -1879,6 +1900,25 @@ __Usage:__
18791900

18801901

18811902

1903+
### Set the object retention policy of a File.
1904+
1905+
Set the object retention policy of a File.
1906+
1907+
View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setObjectRetentionPolicy.js).
1908+
1909+
[![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/setObjectRetentionPolicy.js,samples/README.md)
1910+
1911+
__Usage:__
1912+
1913+
1914+
`node setObjectRetentionPolicy.js <BUCKET_NAME>`
1915+
1916+
1917+
-----
1918+
1919+
1920+
1921+
18821922
### Set Public Access Prevention Enforced
18831923

18841924
View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/setPublicAccessPreventionEnforced.js).
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// sample-metadata:
18+
// title: Create a Bucket with object retention enabled.
19+
// description: Create a Bucket with object retention enabled.
20+
// usage: node createBucketWithObjectRetention.js <BUCKET_NAME>
21+
22+
function main(bucketName = 'my-bucket') {
23+
// [START storage_create_bucket_with_object_retention]
24+
/**
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// The ID of your GCS bucket
28+
// const bucketName = 'your-unique-bucket-name';
29+
30+
// Imports the Google Cloud client library
31+
const {Storage} = require('@google-cloud/storage');
32+
33+
// Creates a client
34+
// The bucket in the sample below will be created in the project associated with this client.
35+
// For more information, please see https://cloud.google.com/docs/authentication/production or https://googleapis.dev/nodejs/storage/latest/Storage.html
36+
const storage = new Storage();
37+
38+
async function createBucketWithObjectRetention() {
39+
const [bucket] = await storage.createBucket(bucketName, {
40+
enableObjectRetention: true,
41+
});
42+
43+
console.log(
44+
`Created '${bucket.name}' with object retention enabled setting: ${bucket.metadata.objectRetention.mode}`
45+
);
46+
}
47+
48+
createBucketWithObjectRetention().catch(console.error);
49+
// [END storage_create_bucket_with_object_retention]
50+
}
51+
52+
process.on('unhandledRejection', err => {
53+
console.error(err.message);
54+
process.exitCode = 1;
55+
});
56+
main(...process.argv.slice(2));

samples/downloaded.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

samples/setObjectRetentionPolicy.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// sample-metadata:
18+
// title: Set the object retention policy of a File.
19+
// description: Set the object retention policy of a File.
20+
// usage: node setObjectRetentionPolicy.js <BUCKET_NAME>
21+
22+
function main(
23+
bucketName = 'my-bucket',
24+
destFileName = 'file.txt',
25+
contents = 'this is the file content'
26+
) {
27+
// [START storage_set_object_retention_policy]
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// The ID of your GCS bucket
32+
// const bucketName = 'your-unique-bucket-name';
33+
34+
// The new ID for your GCS file
35+
// const destFileName = 'your-new-file-name';
36+
37+
// The content to be uploaded in the GCS file
38+
// const contents = 'your file content';
39+
40+
// Imports the Google Cloud client library
41+
const {Storage} = require('@google-cloud/storage');
42+
43+
// Creates a client
44+
// The bucket in the sample below will be created in the project associated with this client.
45+
// For more information, please see https://cloud.google.com/docs/authentication/production or https://googleapis.dev/nodejs/storage/latest/Storage.html
46+
const storage = new Storage();
47+
48+
async function setObjectRetentionPolicy() {
49+
// Get a reference to the bucket
50+
const myBucket = storage.bucket(bucketName);
51+
52+
// Create a reference to a file object
53+
const file = myBucket.file(destFileName);
54+
55+
// Save the file data
56+
await file.save(contents);
57+
58+
// Set the retention policy for the file
59+
const retentionDate = new Date();
60+
retentionDate.setDate(retentionDate.getDate() + 10);
61+
const [metadata] = await file.setMetadata({
62+
retention: {
63+
mode: 'Unlocked',
64+
retainUntilTime: retentionDate.toISOString(),
65+
},
66+
});
67+
68+
console.log(
69+
`Retention policy for file ${file.name} was set to: ${metadata.retention.mode}`
70+
);
71+
72+
// To modify an existing policy on an unlocked file object, pass in the override parameter
73+
const newRetentionDate = new Date();
74+
retentionDate.setDate(retentionDate.getDate() + 9);
75+
[metdata] = await file.setMetadata({
76+
retention: {retainUntilTime: newRetentionDate},
77+
overrideUnlockedRetention: true,
78+
});
79+
80+
console.log(
81+
`Retention policy for file ${file.name} was updated to: ${metadata.retention.retainUntilTime}`
82+
);
83+
}
84+
85+
setObjectRetentionPolicy().catch(console.error);
86+
// [END storage_set_object_retention_policy]
87+
}
88+
89+
process.on('unhandledRejection', err => {
90+
console.error(err.message);
91+
process.exitCode = 1;
92+
});
93+
main(...process.argv.slice(2));

samples/system-test/buckets.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const bucketNameDualRegion = `${samplesTestBucketPrefix}-b`;
2929
const bucketNameDualRegionTurbo = `${samplesTestBucketPrefix}-c`;
3030
const bucketNameWithClassAndLocation = `${samplesTestBucketPrefix}-d`;
3131
const bucketNameAutoclass = `${samplesTestBucketPrefix}-e`;
32+
const bucketNameObjectRetention = `${samplesTestBucketPrefix}-f`;
3233
const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA;
3334
const bucket = storage.bucket(bucketName);
3435
const bucketWithClassAndLocation = storage.bucket(
3536
bucketNameWithClassAndLocation
3637
);
3738
const dualRegionBucket = storage.bucket(bucketNameDualRegion);
3839
const dualRegionBucketTurbo = storage.bucket(bucketNameDualRegionTurbo);
40+
const objectRetentionBucket = storage.bucket(bucketNameObjectRetention);
3941

4042
const PUBLIC_ACCESS_PREVENTION_INHERITED = 'inherited';
4143
const PUBLIC_ACCESS_PREVENTION_ENFORCED = 'enforced';
@@ -417,3 +419,16 @@ it('should delete a bucket', async () => {
417419
const [exists] = await bucket.exists();
418420
assert.strictEqual(exists, false);
419421
});
422+
423+
it('should create a bucket with object retention enabled', async () => {
424+
const output = execSync(
425+
`node createBucketWithObjectRetention.js ${bucketNameObjectRetention}`
426+
);
427+
console.log(output);
428+
assert.include(
429+
output,
430+
`Created '${bucketNameObjectRetention}' with object retention enabled setting: Enabled`
431+
);
432+
const [metadata] = await objectRetentionBucket.getMetadata();
433+
assert.strictEqual(metadata.objectRetention.mode, 'Enabled');
434+
});

samples/system-test/files.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const storage = new Storage();
3030
const cwd = path.join(__dirname, '..');
3131
const bucketName = generateName();
3232
const bucket = storage.bucket(bucketName);
33+
const objectRetentionBucketName = generateName();
34+
const objectRetentionBucket = storage.bucket(objectRetentionBucketName);
3335
const fileContents = 'these-are-my-contents';
3436
const fileName = 'test.txt';
3537
const memoryFileName = 'testmemory.txt';
@@ -576,6 +578,25 @@ describe('file', () => {
576578
assert.strictEqual(exists, false);
577579
});
578580
});
581+
582+
describe('Object Retention', () => {
583+
before(async () => {
584+
await storage.createBucket(objectRetentionBucketName, {
585+
enableObjectRetention: true,
586+
});
587+
});
588+
589+
it('should create a file with unlocked retention and then override it', async () => {
590+
const output = execSync(
591+
`node setObjectRetentionPolicy.js ${objectRetentionBucketName} ${fileName} ${fileContent}`
592+
);
593+
assert.include(output, 'Retention policy for file');
594+
const file = objectRetentionBucket.file(fileName);
595+
const [metadata] = await file.getMetadata();
596+
assert(metadata.retention.retainUntilTime);
597+
assert(metadata.retention.mode.toUpperCase(), 'UNLOCKED');
598+
});
599+
});
579600
});
580601

581602
function generateName() {

0 commit comments

Comments
 (0)