Skip to content
Draft
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
72 changes: 72 additions & 0 deletions storage/composeFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 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 compose/combine multiple files into a
* single destination file with the Google Cloud Storage API.
*
* For more information, see the README.md under /storage and the documentation
* at https://cloud.google.com/storage/docs.
*/
Comment on lines +17 to +23

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The JSDoc comment describes Access Control Lists (ACLs), which is incorrect and appears to be a copy-paste error. It should be updated to accurately describe composing/combining files.

Suggested change
/**
* This application demonstrates how to perform basic operations on bucket and
* file Access Control Lists with the Google Cloud Storage API.
*
* For more information, see the README.md under /storage and the documentation
* at https://cloud.google.com/storage/docs.
*/
/**
* This application demonstrates how to compose/combine multiple files into a
* single destination file with the Google Cloud Storage API.
*
* For more information, see the README.md under /storage and the documentation
* at https://cloud.google.com/storage/docs.
*/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored by AI Agent


function main(
bucketName = 'my-bucket',
firstFileName = 'first.txt',
secondFileName = 'second.txt',
destinationFileName = 'destination.txt',
deleteSourceObjects = false
) {
// [START storage_compose_file]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'your-unique-bucket-name';
// const firstFileName = 'first.txt';
// const secondFileName = 'second.txt';
// const destinationFileName = 'destination.txt';
// const deleteSourceObjects = false;

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

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

async function composeFile() {
const bucket = storage.bucket(bucketName);
const sources = [firstFileName, secondFileName];

await bucket.combine(sources, destinationFileName);

console.log(
`New composite file ${destinationFileName} was created by combining ${sources.join(', ')}`
);
Comment on lines +54 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of hardcoding the file names in the log message, use sources.join(', '). This makes the code more robust and maintainable if the sources array is modified in the future.

Suggested change
console.log(
`New composite file ${destinationFileName} was created by combining ${firstFileName} and ${secondFileName}`
);
console.log(
`New composite file ${destinationFileName} was created by combining ${sources.join(', ')}`
);


if (String(deleteSourceObjects) === 'true') {
await Promise.all(
sources.map(async source => {
await bucket.file(source).delete();
})
);
console.log(`Deleted source objects: ${sources.join(', ')}`);
}
}

composeFile().catch(console.error);
// [END storage_compose_file]
}

main(...process.argv.slice(2));
51 changes: 51 additions & 0 deletions storage/system-test/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,57 @@ describe('file', () => {
await bucket.file(noContextFileName).delete();
});
});

describe('compose file', () => {
it('should combine multiple files into one new file and optionally delete source objects', async () => {
const firstFileName = 'first.txt';
const secondFileName = 'second.txt';
const destinationFileName = 'destination.txt';

// upload test.txt twice
await bucket.upload(filePath, {destination: firstFileName});
await bucket.upload(filePath, {destination: secondFileName});

// Test with deleteSourceObjects = false
let output = execSync(
`node composeFile.js ${bucketName} ${firstFileName} ${secondFileName} ${destinationFileName} false`
);
assert.include(
output,
`New composite file ${destinationFileName} was created by combining ${firstFileName}, ${secondFileName}`
);
assert.notInclude(output, 'Deleted source objects');

let [destExists] = await bucket.file(destinationFileName).exists();
assert.strictEqual(destExists, true);
let [firstExists] = await bucket.file(firstFileName).exists();
assert.strictEqual(firstExists, true);
let [secondExists] = await bucket.file(secondFileName).exists();
assert.strictEqual(secondExists, true);

await bucket.file(destinationFileName).delete();

// Test with deleteSourceObjects = true
output = execSync(
`node composeFile.js ${bucketName} ${firstFileName} ${secondFileName} ${destinationFileName} true`
);
assert.include(
output,
`New composite file ${destinationFileName} was created by combining ${firstFileName}, ${secondFileName}`
);
assert.include(
output,
`Deleted source objects: ${firstFileName}, ${secondFileName}`
);

[destExists] = await bucket.file(destinationFileName).exists();
assert.strictEqual(destExists, true);
[firstExists] = await bucket.file(firstFileName).exists();
assert.strictEqual(firstExists, false);
[secondExists] = await bucket.file(secondFileName).exists();
assert.strictEqual(secondExists, false);
});
});
});

function generateName() {
Expand Down
Loading