Skip to content

Commit

Permalink
samples: added samples for upload from / download into memory (#1731)
Browse files Browse the repository at this point in the history
* samples: added samples for upload from / download into memory

* 馃 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
shaffeeullah and gcf-owl-bot[bot] committed Dec 10, 2021
1 parent 8ecb70e commit 29f91cb
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -130,6 +130,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| Download Encrypted File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadEncryptedFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadEncryptedFile.js,samples/README.md) |
| Download File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadFile.js,samples/README.md) |
| Download File Using Requester Pays | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadFileUsingRequesterPays.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadFileUsingRequesterPays.js,samples/README.md) |
| Download Into Memory | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadIntoMemory.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadIntoMemory.js,samples/README.md) |
| Storage Download Public File. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadPublicFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadPublicFile.js,samples/README.md) |
| Enable Bucket Lifecycle Management | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/enableBucketLifecycleManagement.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/enableBucketLifecycleManagement.js,samples/README.md) |
| Storage Enable Bucket Versioning. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/enableBucketVersioning.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/enableBucketVersioning.js,samples/README.md) |
Expand Down Expand Up @@ -198,6 +199,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| Upload Encrypted File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadEncryptedFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadEncryptedFile.js,samples/README.md) |
| Upload File | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadFile.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadFile.js,samples/README.md) |
| Upload File With Kms Key | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadFileWithKmsKey.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadFileWithKmsKey.js,samples/README.md) |
| Upload From Memory | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadFromMemory.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadFromMemory.js,samples/README.md) |
| Upload Without Authentication Resumable | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadWithoutAuthenticationResumable.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadWithoutAuthenticationResumable.js,samples/README.md) |
| Upload Without Authentication Signed Url | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadWithoutAuthenticationSignedUrl.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadWithoutAuthenticationSignedUrl.js,samples/README.md) |
| View Bucket Iam Members | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/viewBucketIamMembers.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/viewBucketIamMembers.js,samples/README.md) |
Expand Down
36 changes: 36 additions & 0 deletions samples/README.md
Expand Up @@ -48,6 +48,7 @@ objects to users via direct download.
* [Download Encrypted File](#download-encrypted-file)
* [Download File](#download-file)
* [Download File Using Requester Pays](#download-file-using-requester-pays)
* [Download Into Memory](#download-into-memory)
* [Storage Download Public File.](#storage-download-public-file.)
* [Enable Bucket Lifecycle Management](#enable-bucket-lifecycle-management)
* [Storage Enable Bucket Versioning.](#storage-enable-bucket-versioning.)
Expand Down Expand Up @@ -116,6 +117,7 @@ objects to users via direct download.
* [Upload Encrypted File](#upload-encrypted-file)
* [Upload File](#upload-file)
* [Upload File With Kms Key](#upload-file-with-kms-key)
* [Upload From Memory](#upload-from-memory)
* [Upload Without Authentication Resumable](#upload-without-authentication-resumable)
* [Upload Without Authentication Signed Url](#upload-without-authentication-signed-url)
* [View Bucket Iam Members](#view-bucket-iam-members)
Expand Down Expand Up @@ -667,6 +669,23 @@ __Usage:__



### Download Into Memory

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadIntoMemory.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/downloadIntoMemory.js,samples/README.md)

__Usage:__


`node samples/downloadIntoMemory.js`


-----




### Storage Download Public File.

Download Public File.
Expand Down Expand Up @@ -1857,6 +1876,23 @@ __Usage:__



### Upload From Memory

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadFromMemory.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/uploadFromMemory.js,samples/README.md)

__Usage:__


`node samples/uploadFromMemory.js`


-----




### Upload Without Authentication Resumable

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/uploadWithoutAuthenticationResumable.js).
Expand Down
56 changes: 56 additions & 0 deletions samples/downloadIntoMemory.js
@@ -0,0 +1,56 @@
// Copyright 2021 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.

/**
* This application demonstrates how to perform basic operations on files 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.
*/

function main(bucketName = 'my-bucket', fileName = 'test.txt') {
// [START storage_file_download_into_memory]
/**
* 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 downloadIntoMemory() {
// Downloads the file into a buffer in memory.
const contents = await storage.bucket(bucketName).file(fileName).download();

console.log(
`Contents of gs://${bucketName}/${fileName} are ${contents.toString()}.`
);
}

downloadIntoMemory().catch(console.error);
// [END storage_file_download_into_memory]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
27 changes: 27 additions & 0 deletions samples/system-test/files.test.js
Expand Up @@ -32,6 +32,7 @@ const bucketName = generateName();
const bucket = storage.bucket(bucketName);
const fileContents = 'these-are-my-contents';
const fileName = 'test.txt';
const memoryFileName = 'testmemory.txt';
const movedFileName = 'test2.txt';
const copiedFileName = 'test3.txt';
const renamedFileName = 'test4.txt';
Expand Down Expand Up @@ -65,6 +66,20 @@ describe('file', () => {
assert.strictEqual(exists, true);
});

it('should upload a file from memory', async () => {
const output = execSync(
`node uploadFromMemory.js ${bucketName} ${fileContents} ${memoryFileName}`
);
assert.match(
output,
new RegExp(
`${memoryFileName} with contents ${fileContents} uploaded to ${bucketName}.`
)
);
const [exists] = await bucket.file(memoryFileName).exists();
assert.strictEqual(exists, true);
});

it('should upload a file without authentication using resumable strategy', async () => {
const output = execSync(
`node uploadWithoutAuthenticationResumable.js ${bucketName} ${fileContents} ${fileName}`
Expand Down Expand Up @@ -162,6 +177,18 @@ describe('file', () => {
fs.statSync(downloadFilePath);
});

it('should download a file into memory', () => {
const output = execSync(
`node downloadIntoMemory.js ${bucketName} ${memoryFileName}`
);
assert.match(
output,
new RegExp(
`Contents of gs://${bucketName}/${memoryFileName} are ${fileContents}.`
)
);
});

it('should download a file using a stream', () => {
const output = execSync(
`node streamFileDownload.js ${bucketName} ${fileName} ${downloadFilePath}`
Expand Down
54 changes: 54 additions & 0 deletions samples/uploadFromMemory.js
@@ -0,0 +1,54 @@
// Copyright 2021 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.

function main(
bucketName = 'my-bucket',
contents = 'these are my file contents',
destFileName = 'file.txt'
) {
// [START storage_file_upload_from_memory]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The contents that you want to upload
// const contents = 'these are my contents';

// The new ID for your GCS file
// const destFileName = 'your-new-file-name';

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

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

async function uploadFromMemory() {
await storage.bucket(bucketName).file(destFileName).save(contents);

console.log(
`${destFileName} with contents ${contents} uploaded to ${bucketName}.`
);
}

uploadFromMemory().catch(console.error);
// [END storage_file_upload_from_memory]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));

0 comments on commit 29f91cb

Please sign in to comment.