Skip to content
Open
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
57 changes: 57 additions & 0 deletions storage/disableDefaultEventBasedHold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 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';

/**
* This application demonstrates how to use Bucket Lock operations on buckets
* and objects using the Google Cloud Storage API.
*
* For more information read the documentation
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket') {
// [START storage_disable_default_event_based_hold]
/**
* 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 disableDefaultEventBasedHold() {
try {
// Disables a default event-based hold for a bucket.
await storage.bucket(bucketName).setMetadata({
defaultEventBasedHold: false,
});
console.log(`Default event-based hold was disabled for ${bucketName}.`);
} catch (error) {
console.error(
'Error executing disable default event-based hold:',
error.message || error
);
}
}

disableDefaultEventBasedHold();
// [END storage_disable_default_event_based_hold]
}
main(...process.argv.slice(2));
59 changes: 59 additions & 0 deletions storage/enableDefaultEventBasedHold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 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';

/**
* This application demonstrates how to use Bucket Lock operations on buckets
* and objects using the Google Cloud Storage API.
*
* For more information read the documentation
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket') {
// [START storage_enable_default_event_based_hold]

/**
* 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 enableDefaultEventBasedHold() {
try {
// Enables a default event-based hold for the bucket.
await storage.bucket(bucketName).setMetadata({
defaultEventBasedHold: true,
});

console.log(`Default event-based hold was enabled for ${bucketName}.`);
} catch (error) {
console.error(
'Error executing enable default event-based hold:',
error.message || error
);
}
}

enableDefaultEventBasedHold();
// [END storage_enable_default_event_based_hold]
}
main(...process.argv.slice(2));
56 changes: 56 additions & 0 deletions storage/getDefaultEventBasedHold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 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';

/**
* This application demonstrates how to use Bucket Lock operations on buckets
* and objects using the Google Cloud Storage API.
*
* For more information read the documentation
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket') {
// [START storage_get_default_event_based_hold]
/**
* 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 getDefaultEventBasedHold() {
try {
const [metadata] = await storage.bucket(bucketName).getMetadata();
console.log(
`Default event-based hold: ${metadata.defaultEventBasedHold}.`
);
} catch (error) {
console.error(
'Error executing get default event-based hold:',
error.message || error
);
}
}

getDefaultEventBasedHold();
// [END storage_get_default_event_based_hold]
}
main(...process.argv.slice(2));
64 changes: 64 additions & 0 deletions storage/getRetentionPolicy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 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';

/**
* This application demonstrates how to use Bucket Lock operations on buckets
* and objects using the Google Cloud Storage API.
*
* For more information read the documentation
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket') {
// [START storage_get_retention_policy]
/**
* 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 getRetentionPolicy() {
try {
const [metadata] = await storage.bucket(bucketName).getMetadata();
if (metadata.retentionPolicy) {
const retentionPolicy = metadata.retentionPolicy;
console.log('A retention policy exists!');
console.log(`Period: ${retentionPolicy.retentionPeriod}`);
console.log(`Effective time: ${retentionPolicy.effectiveTime}`);
if (retentionPolicy.isLocked) {
console.log('Policy is locked');
} else {
console.log('Policy is unlocked');
}
}
} catch (error) {
console.error(
'Error executing get bucket retention policy:',
error.message || error
);
}
}

getRetentionPolicy();
// [END storage_get_retention_policy]
}
main(...process.argv.slice(2));
65 changes: 65 additions & 0 deletions storage/lockRetentionPolicy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 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';

/**
* This application demonstrates how to use Bucket Lock operations on buckets
* and objects using the Google Cloud Storage API.
*
* For more information read the documentation
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket') {
// [START storage_lock_retention_policy]
/**
* 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 lockRetentionPolicy() {
try {
// Gets the current metageneration value for the bucket, required by
// lock_retention_policy
const [unlockedMetadata] = await storage.bucket(bucketName).getMetadata();

// Warning: Once a retention policy is locked, it cannot be unlocked. The
// retention period can only be increased
const [lockedMetadata] = await storage
.bucket(bucketName)
.lock(unlockedMetadata.metageneration);
console.log(`Retention policy for ${bucketName} is now locked`);
console.log(
`Retention policy effective as of ${lockedMetadata.retentionPolicy.effectiveTime}`
);
} catch (error) {
console.error(
'Error executing lock bucket retention policy:',
error.message || error
);
}
}

lockRetentionPolicy();
// [END storage_lock_retention_policy]
}
main(...process.argv.slice(2));
30 changes: 30 additions & 0 deletions storage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@google-cloud/storage-samples",
"description": "Samples for the Cloud Storage Client Library for Node.js.",
"license": "Apache-2.0",
"author": "Google Inc.",
"engines": {
"node": ">=12"
},
"repository": "googleapis/nodejs-storage",
"private": true,
"files": [
"*.js"
],
"scripts": {
"cleanup": "node scripts/cleanup",
"test": "mocha system-test/*.js --timeout 800000"
},
"dependencies": {
"@google-cloud/pubsub": "^4.0.0",
"@google-cloud/storage": "^7.19.0",
"node-fetch": "^2.6.7",
"uuid": "^8.0.0",
"yargs": "^16.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^8.0.0",
"p-limit": "^3.1.0"
}
}
Loading
Loading