Skip to content

Commit

Permalink
samples: add preconditions to storage.objects.rewrite, storage.object…
Browse files Browse the repository at this point in the history
…s.insert, storage.objects.patch, & storage.objects.delete samples (#2046)

* started copy sample

* samples: add preconditions to storage.objects.rewrite & storage.objects.delete samples

* linted files

* samples: storage.objects.insert precondition samples

* updated test

* samples: storage.objects.insert precondition samples (#2047)

* test: add retries (#2039)

* refactor: remove unused `restart` private method (#2038)

Co-authored-by: Sameena Shaffeeullah <shaffeeullah@google.com>

* fix: Retry `EPIPE` Connection Errors + Attempt Retries in Resumable Upload Connection Errors (#2040)

* feat: Add `epipe` as retryable error

* fix: capture and retry potential connection errors

* test: Add tests and remove logs

* test: set `retryOptions` by copy rather than reference

* fix: grammar

* chore(main): release 6.4.1 (#2036)

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>

* test: add delay to bucket tests to reduce rate limiting errors (#2043)

* samples: storage.objects.insert precondition samples

* updated test

Co-authored-by: Daniel Bankhead <danielbankhead@google.com>
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com>

* Revert "samples: storage.objects.insert precondition samples (#2047)" (#2048)

This reverts commit ca1b4b7.

* storage.objects.patch sample updates

* destinationGenerationMatchPrecondition

* renamed variable

Co-authored-by: Daniel Bankhead <danielbankhead@google.com>
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 18, 2022
1 parent e58ee0b commit ac2de41
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 78 deletions.
17 changes: 15 additions & 2 deletions samples/changeFileCSEKToCMEK.js
Expand Up @@ -23,7 +23,8 @@ function main(
bucketName = 'my-bucket',
fileName = 'test.txt',
encryptionKey = 'my-encription-key',
kmsKeyName = 'my-kms-key'
kmsKeyName = 'my-kms-key',
generationMatchPrecondition = 0
) {
// [START storage_object_csek_to_cmek]
/**
Expand All @@ -49,13 +50,25 @@ function main(
const storage = new Storage();

async function changeFileCSEKToCMEK() {
const rotateEncryptionKeyOptions = {
kmsKeyName,
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
preconditionOpts: {
ifGenerationMatch: generationMatchPrecondition,
},
};

console.log(rotateEncryptionKeyOptions);

await storage
.bucket(bucketName)
.file(fileName, {
encryptionKey: Buffer.from(encryptionKey, 'base64'),
})
.rotateEncryptionKey({
kmsKeyName,
rotateEncryptionKeyOptions,
});

console.log(
Expand Down
15 changes: 13 additions & 2 deletions samples/composeFile.js
Expand Up @@ -23,7 +23,8 @@ function main(
bucketName = 'my-bucket',
firstFileName = 'file-one.txt',
secondFileName = 'file-two.txt',
destinationFileName = 'file-one-two.txt'
destinationFileName = 'file-one-two.txt',
destinationGenerationMatchPrecondition = 0
) {
// [START storage_compose_file]
/**
Expand Down Expand Up @@ -51,7 +52,17 @@ function main(
const bucket = storage.bucket(bucketName);
const sources = [firstFileName, secondFileName];

await bucket.combine(sources, destinationFileName);
// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const combineOptions = {
ifGenerationMatch: destinationGenerationMatchPrecondition,
};
await bucket.combine(sources, destinationFileName, combineOptions);

console.log(
`New composite file ${destinationFileName} was created by combining ${firstFileName} and ${secondFileName}`
Expand Down
20 changes: 18 additions & 2 deletions samples/copyFile.js
Expand Up @@ -24,7 +24,8 @@ function main(
srcBucketName = 'my-bucket',
srcFilename = 'test2.txt',
destBucketName = 'my-bucket',
destFileName = 'test3.txt'
destFileName = 'test3.txt',
destinationGenerationMatchPrecondition = 0
) {
// [START storage_copy_file]
/**
Expand All @@ -49,11 +50,26 @@ function main(
const storage = new Storage();

async function copyFile() {
const copyDestination = storage.bucket(destBucketName).file(destFileName);

// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const copyOptions = {
preconditionOpts: {
ifGenerationMatch: destinationGenerationMatchPrecondition,
},
};

// Copies the file to the other bucket
await storage
.bucket(srcBucketName)
.file(srcFilename)
.copy(storage.bucket(destBucketName).file(destFileName));
.copy(copyDestination, copyOptions);

console.log(
`gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFileName}`
Expand Down
19 changes: 17 additions & 2 deletions samples/copyOldVersionOfFile.js
Expand Up @@ -24,7 +24,8 @@ function main(
srcFilename = 'test2.txt',
destBucketName = 'my-bucket',
destFileName = 'test3.txt',
generation = 1
generation = 1,
destinationGenerationMatchPrecondition = 0
) {
// [START storage_copy_file_archived_generation]
/**
Expand Down Expand Up @@ -53,12 +54,26 @@ function main(

async function copyOldVersionOfFile() {
// Copies the file to the other bucket

// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const copyOptions = {
preconditionOpts: {
ifGenerationMatch: destinationGenerationMatchPrecondition,
},
};

await storage
.bucket(srcBucketName)
.file(srcFilename, {
generation,
})
.copy(storage.bucket(destBucketName).file(destFileName));
.copy(storage.bucket(destBucketName).file(destFileName), copyOptions);

console.log(
`Generation ${generation} of file ${srcFilename} in bucket ${srcBucketName} was copied to ${destFileName} in bucket ${destBucketName}`
Expand Down
18 changes: 16 additions & 2 deletions samples/deleteFile.js
Expand Up @@ -20,7 +20,11 @@
* at https://cloud.google.com/storage/docs.
*/

function main(bucketName = 'my-bucket', fileName = 'test.txt') {
function main(
bucketName = 'my-bucket',
fileName = 'test.txt',
generationMatchPrecondition = 0
) {
// [START storage_delete_file]
/**
* TODO(developer): Uncomment the following lines before running the sample.
Expand All @@ -37,8 +41,18 @@ function main(bucketName = 'my-bucket', fileName = 'test.txt') {
// Creates a client
const storage = new Storage();

// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const deleteOptions = {
ifGenerationMatch: generationMatchPrecondition,
};
async function deleteFile() {
await storage.bucket(bucketName).file(fileName).delete();
await storage.bucket(bucketName).file(fileName).delete(deleteOptions);

console.log(`gs://${bucketName}/${fileName} deleted`);
}
Expand Down
16 changes: 14 additions & 2 deletions samples/fileChangeStorageClass.js
Expand Up @@ -22,7 +22,8 @@
function main(
bucketName = 'my-bucket',
fileName = 'file.txt',
storageClass = 'standard'
storageClass = 'standard',
generationMatchPrecondition = 0
) {
// [START storage_change_file_storage_class]
// Imports the Google Cloud client library
Expand All @@ -46,10 +47,21 @@ function main(
// const storageClass = 'coldline';

async function fileChangeStorageClass() {
// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const setStorageClassOptions = {
ifGenerationMatch: generationMatchPrecondition,
};

await storage
.bucket(bucketName)
.file(fileName)
.setStorageClass(storageClass);
.setStorageClass(storageClass, setStorageClassOptions);

console.log(`${fileName} has been set to ${storageClass}`);
}
Expand Down
39 changes: 27 additions & 12 deletions samples/fileSetMetadata.js
Expand Up @@ -19,7 +19,11 @@
// description: Set file metadata.
// usage: node fileSetMetadata.js <BUCKET_NAME> <FILE_NAME>

function main(bucketName = 'my-bucket', fileName = 'file.txt') {
function main(
bucketName = 'my-bucket',
fileName = 'file.txt',
generationMatchPrecondition = 0
) {
// [START storage_set_metadata]
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
Expand All @@ -37,23 +41,34 @@ function main(bucketName = 'my-bucket', fileName = 'file.txt') {
// const fileName = 'your-file-name';

async function setFileMetadata() {
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
const options = {
ifGenerationMatch: generationMatchPrecondition,
};

// Set file metadata.
const [metadata] = await storage
.bucket(bucketName)
.file(fileName)
.setMetadata({
// Predefined metadata for server e.g. 'cacheControl', 'contentDisposition',
// 'contentEncoding', 'contentLanguage', 'contentType'
contentDisposition: 'attachment; filename*=utf-8\'\'"anotherImage.jpg"',
contentType: 'image/jpeg',
.setMetadata(
{
// Predefined metadata for server e.g. 'cacheControl', 'contentDisposition',
// 'contentEncoding', 'contentLanguage', 'contentType'
contentDisposition:
'attachment; filename*=utf-8\'\'"anotherImage.jpg"',
contentType: 'image/jpeg',

// A note or actionable items for user e.g. uniqueId, object description,
// or other useful information.
metadata: {
description: 'file description...',
modified: '1900-01-01',
// A note or actionable items for user e.g. uniqueId, object description,
// or other useful information.
metadata: {
description: 'file description...',
modified: '1900-01-01',
},
},
});
options
);

console.log(
'Updated metadata for object',
Expand Down
21 changes: 19 additions & 2 deletions samples/moveFile.js
Expand Up @@ -23,7 +23,8 @@
function main(
bucketName = 'my-bucket',
srcFileName = 'test.txt',
destFileName = 'test2.txt'
destFileName = 'test2.txt',
destinationGenerationMatchPrecondition = 0
) {
// [START storage_move_file]
/**
Expand All @@ -45,8 +46,24 @@ function main(
const storage = new Storage();

async function moveFile() {
// Optional:
// Set a generation-match precondition to avoid potential race conditions
// and data corruptions. The request to upload is aborted if the object's
// generation number does not match your precondition. For a destination
// object that does not yet exist, set the ifGenerationMatch precondition to 0
// If the destination object already exists in your bucket, set instead a
// generation-match precondition using its generation number.
const moveOptions = {
preconditionOpts: {
ifGenerationMatch: destinationGenerationMatchPrecondition,
},
};

// Moves the file within the bucket
await storage.bucket(bucketName).file(srcFileName).move(destFileName);
await storage
.bucket(bucketName)
.file(srcFileName)
.move(destFileName, moveOptions);

console.log(
`gs://${bucketName}/${srcFileName} moved to gs://${bucketName}/${destFileName}`
Expand Down
22 changes: 18 additions & 4 deletions samples/releaseEventBasedHold.js
Expand Up @@ -20,7 +20,11 @@
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket', fileName = 'test.txt') {
function main(
bucketName = 'my-bucket',
fileName = 'test.txt',
generationMatchPrecondition = 0
) {
// [START storage_release_event_based_hold]
/**
* TODO(developer): Uncomment the following lines before running the sample.
Expand All @@ -38,9 +42,19 @@ function main(bucketName = 'my-bucket', fileName = 'test.txt') {
const storage = new Storage();

async function releaseEventBasedHold() {
await storage.bucket(bucketName).file(fileName).setMetadata({
eventBasedHold: false,
});
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
const options = {
ifGenerationMatch: generationMatchPrecondition,
};

await storage.bucket(bucketName).file(fileName).setMetadata(
{
eventBasedHold: false,
},
options
);
console.log(`Event-based hold was released for ${fileName}.`);
}

Expand Down
22 changes: 18 additions & 4 deletions samples/releaseTemporaryHold.js
Expand Up @@ -20,7 +20,11 @@
* at https://cloud.google.com/storage/docs/bucket-lock
*/

function main(bucketName = 'my-bucket', fileName = 'test.txt') {
function main(
bucketName = 'my-bucket',
fileName = 'test.txt',
generationMatchPrecondition = 0
) {
// [START storage_release_temporary_hold]
/**
* TODO(developer): Uncomment the following lines before running the sample.
Expand All @@ -38,9 +42,19 @@ function main(bucketName = 'my-bucket', fileName = 'test.txt') {
const storage = new Storage();

async function releaseTemporaryHold() {
await storage.bucket(bucketName).file(fileName).setMetadata({
temporaryHold: false,
});
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
const options = {
ifGenerationMatch: generationMatchPrecondition,
};

await storage.bucket(bucketName).file(fileName).setMetadata(
{
temporaryHold: false,
},
options
);
console.log(`Temporary hold was released for ${fileName}.`);
}

Expand Down

0 comments on commit ac2de41

Please sign in to comment.