Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 48e4bc9

Browse files
authored
samples: add contents variable to streamFileUpload sample (#2068)
* samples: add contents variable to streamFileUpload sample * check that contents of upload match
1 parent fbe2deb commit 48e4bc9

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

samples/streamFileUpload.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
* at https://cloud.google.com/storage/docs.
2121
*/
2222

23-
function main(bucketName = 'my-bucket', destFileName = 'file.txt') {
23+
function main(
24+
bucketName = 'my-bucket',
25+
destFileName = 'file.txt',
26+
contents = 'this is the file content'
27+
) {
2428
// [START storage_stream_file_upload]
2529
/**
2630
* TODO(developer): Uncomment the following lines before running the sample
@@ -31,6 +35,9 @@ function main(bucketName = 'my-bucket', destFileName = 'file.txt') {
3135
// The new ID for your GCS file
3236
// const destFileName = 'your-new-file-name';
3337

38+
// The content to be uploaded in the GCS file
39+
// const contents = 'your file content';
40+
3441
// Imports the Google Cloud client library
3542
const {Storage} = require('@google-cloud/storage');
3643

@@ -48,7 +55,7 @@ function main(bucketName = 'my-bucket', destFileName = 'file.txt') {
4855

4956
// Create a pass through stream from a string
5057
const passthroughStream = new stream.PassThrough();
51-
passthroughStream.write('input text');
58+
passthroughStream.write(contents);
5259
passthroughStream.end();
5360

5461
async function streamFileUpload() {

samples/system-test/files.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@ describe('file', () => {
103103

104104
it('should upload a file using a stream', async () => {
105105
const output = execSync(
106-
`node streamFileUpload.js ${bucketName} ${fileName}`
106+
`node streamFileUpload.js ${bucketName} ${fileName} ${fileContents}`
107107
);
108108
assert.match(output, new RegExp(`${fileName} uploaded to ${bucketName}`));
109109
const [exists] = await bucket.file(fileName).exists();
110110
assert.strictEqual(exists, true);
111+
const response = await bucket.file(fileName).download();
112+
assert.strictEqual(response[0].toString(), fileContents);
111113
});
112114

113115
it('should upload a file with a kms key', async () => {

0 commit comments

Comments
 (0)