Skip to content
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));
73 changes: 73 additions & 0 deletions storage/releaseEventBasedHold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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',
fileName = 'test.txt',
metagenerationMatchPrecondition = null
) {
// [START storage_release_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';

// The ID of your GCS file
// const fileName = 'your-file-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function releaseEventBasedHold() {
try {
// Optional: set a meta-generation-match precondition to avoid potential race
// conditions and data corruptions. The request to set metadata is aborted if the
// object's metageneration number does not match your precondition.
const options = {
ifMetagenerationMatch: metagenerationMatchPrecondition,
};

await storage.bucket(bucketName).file(fileName).setMetadata(
{
eventBasedHold: false,
},
options
);
console.log(`Event-based hold was released for ${fileName}.`);
} catch (error) {
console.error(
'Error executing release event-based hold:',
error.message || error
);
}
}

releaseEventBasedHold();
// [END storage_release_event_based_hold]
}
main(...process.argv.slice(2));
Loading
Loading