Skip to content

Commit 6f9712f

Browse files
committed
feat(storage): introduce bucket lock samples and tests for migration
1 parent 3b66abe commit 6f9712f

19 files changed

Lines changed: 940 additions & 0 deletions
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to use Bucket Lock operations on buckets
19+
* and objects using the Google Cloud Storage API.
20+
*
21+
* For more information read the documentation
22+
* at https://cloud.google.com/storage/docs/bucket-lock
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_disable_default_event_based_hold]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function disableDefaultEventBasedHold() {
40+
try {
41+
// Disables a default event-based hold for a bucket.
42+
await storage.bucket(bucketName).setMetadata({
43+
defaultEventBasedHold: false,
44+
});
45+
console.log(`Default event-based hold was disabled for ${bucketName}.`);
46+
} catch (error) {
47+
console.error(
48+
'Error executing disable default event-based hold:',
49+
error.message || error
50+
);
51+
}
52+
}
53+
54+
disableDefaultEventBasedHold();
55+
// [END storage_disable_default_event_based_hold]
56+
}
57+
main(...process.argv.slice(2));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to use Bucket Lock operations on buckets
19+
* and objects using the Google Cloud Storage API.
20+
*
21+
* For more information read the documentation
22+
* at https://cloud.google.com/storage/docs/bucket-lock
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_enable_default_event_based_hold]
27+
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+
// Imports the Google Cloud client library
35+
const {Storage} = require('@google-cloud/storage');
36+
37+
// Creates a client
38+
const storage = new Storage();
39+
40+
async function enableDefaultEventBasedHold() {
41+
try {
42+
// Enables a default event-based hold for the bucket.
43+
await storage.bucket(bucketName).setMetadata({
44+
defaultEventBasedHold: true,
45+
});
46+
47+
console.log(`Default event-based hold was enabled for ${bucketName}.`);
48+
} catch (error) {
49+
console.error(
50+
'Error executing enable default event-based hold:',
51+
error.message || error
52+
);
53+
}
54+
}
55+
56+
enableDefaultEventBasedHold();
57+
// [END storage_enable_default_event_based_hold]
58+
}
59+
main(...process.argv.slice(2));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to use Bucket Lock operations on buckets
19+
* and objects using the Google Cloud Storage API.
20+
*
21+
* For more information read the documentation
22+
* at https://cloud.google.com/storage/docs/bucket-lock
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_get_default_event_based_hold]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function getDefaultEventBasedHold() {
40+
try {
41+
const [metadata] = await storage.bucket(bucketName).getMetadata();
42+
console.log(
43+
`Default event-based hold: ${metadata.defaultEventBasedHold}.`
44+
);
45+
} catch (error) {
46+
console.error(
47+
'Error executing get default event-based hold:',
48+
error.message || error
49+
);
50+
}
51+
}
52+
53+
getDefaultEventBasedHold();
54+
// [END storage_get_default_event_based_hold]
55+
}
56+
main(...process.argv.slice(2));

storage/getRetentionPolicy.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to use Bucket Lock operations on buckets
19+
* and objects using the Google Cloud Storage API.
20+
*
21+
* For more information read the documentation
22+
* at https://cloud.google.com/storage/docs/bucket-lock
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_get_retention_policy]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function getRetentionPolicy() {
40+
try {
41+
const [metadata] = await storage.bucket(bucketName).getMetadata();
42+
if (metadata.retentionPolicy) {
43+
const retentionPolicy = metadata.retentionPolicy;
44+
console.log('A retention policy exists!');
45+
console.log(`Period: ${retentionPolicy.retentionPeriod}`);
46+
console.log(`Effective time: ${retentionPolicy.effectiveTime}`);
47+
if (retentionPolicy.isLocked) {
48+
console.log('Policy is locked');
49+
} else {
50+
console.log('Policy is unlocked');
51+
}
52+
}
53+
} catch (error) {
54+
console.error(
55+
'Error executing get bucket retention policy:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
getRetentionPolicy();
62+
// [END storage_get_retention_policy]
63+
}
64+
main(...process.argv.slice(2));

storage/lockRetentionPolicy.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* This application demonstrates how to use Bucket Lock operations on buckets
19+
* and objects using the Google Cloud Storage API.
20+
*
21+
* For more information read the documentation
22+
* at https://cloud.google.com/storage/docs/bucket-lock
23+
*/
24+
25+
function main(bucketName = 'my-bucket') {
26+
// [START storage_lock_retention_policy]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of your GCS bucket
31+
// const bucketName = 'your-unique-bucket-name';
32+
33+
// Imports the Google Cloud client library
34+
const {Storage} = require('@google-cloud/storage');
35+
36+
// Creates a client
37+
const storage = new Storage();
38+
39+
async function lockRetentionPolicy() {
40+
try {
41+
// Gets the current metageneration value for the bucket, required by
42+
// lock_retention_policy
43+
const [unlockedMetadata] = await storage.bucket(bucketName).getMetadata();
44+
45+
// Warning: Once a retention policy is locked, it cannot be unlocked. The
46+
// retention period can only be increased
47+
const [lockedMetadata] = await storage
48+
.bucket(bucketName)
49+
.lock(unlockedMetadata.metageneration);
50+
console.log(`Retention policy for ${bucketName} is now locked`);
51+
console.log(
52+
`Retention policy effective as of ${lockedMetadata.retentionPolicy.effectiveTime}`
53+
);
54+
} catch (error) {
55+
console.error(
56+
'Error executing lock bucket retention policy:',
57+
error.message || error
58+
);
59+
}
60+
}
61+
62+
lockRetentionPolicy();
63+
// [END storage_lock_retention_policy]
64+
}
65+
main(...process.argv.slice(2));

storage/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@google-cloud/storage-samples",
3+
"description": "Samples for the Cloud Storage Client Library for Node.js.",
4+
"license": "Apache-2.0",
5+
"author": "Google Inc.",
6+
"engines": {
7+
"node": ">=12"
8+
},
9+
"repository": "googleapis/nodejs-storage",
10+
"private": true,
11+
"files": [
12+
"*.js"
13+
],
14+
"scripts": {
15+
"cleanup": "node scripts/cleanup",
16+
"test": "mocha system-test/*.js --timeout 800000"
17+
},
18+
"dependencies": {
19+
"@google-cloud/pubsub": "^4.0.0",
20+
"@google-cloud/storage": "^7.19.0",
21+
"node-fetch": "^2.6.7",
22+
"uuid": "^8.0.0",
23+
"yargs": "^16.0.0"
24+
},
25+
"devDependencies": {
26+
"chai": "^4.2.0",
27+
"mocha": "^8.0.0",
28+
"p-limit": "^3.1.0"
29+
}
30+
}

0 commit comments

Comments
 (0)