Skip to content

Commit

Permalink
samples: download byte range (#1732)
Browse files Browse the repository at this point in the history
* samples: download byte range

* 馃 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
ddelgrosso1 and gcf-owl-bot[bot] committed Dec 15, 2021
1 parent 29f91cb commit 257f771
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| Disable Default Event Based Hold | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/disableDefaultEventBasedHold.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/disableDefaultEventBasedHold.js,samples/README.md) |
| Disable Requester Pays | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/disableRequesterPays.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/disableRequesterPays.js,samples/README.md) |
| Disable Uniform Bucket Level Access | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/disableUniformBucketLevelAccess.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/disableUniformBucketLevelAccess.js,samples/README.md) |
| Download Byte Range | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadByteRange.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/downloadByteRange.js,samples/README.md) |
| 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) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ objects to users via direct download.
* [Disable Default Event Based Hold](#disable-default-event-based-hold)
* [Disable Requester Pays](#disable-requester-pays)
* [Disable Uniform Bucket Level Access](#disable-uniform-bucket-level-access)
* [Download Byte Range](#download-byte-range)
* [Download Encrypted File](#download-encrypted-file)
* [Download File](#download-file)
* [Download File Using Requester Pays](#download-file-using-requester-pays)
Expand Down Expand Up @@ -618,6 +619,23 @@ __Usage:__



### Download Byte Range

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadByteRange.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/downloadByteRange.js,samples/README.md)

__Usage:__


`node samples/downloadByteRange.js`


-----




### Download Encrypted File

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/downloadEncryptedFile.js).
Expand Down
81 changes: 81 additions & 0 deletions samples/downloadByteRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* 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 buckets 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.
*/
const path = require('path');
const cwd = path.join(__dirname, '..');

function main(
bucketName = 'my-bucket',
fileName = 'test.txt',
startByte = 0,
endByte = 20,
destFileName = path.join(cwd, 'downloaded.txt')
) {
// [START storage_download_byte_range]
/**
* 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';

// The starting byte at which to begin the download
// const startByte = 0;

// The ending byte at which to end the download
// const endByte = 20;

// The path to which the file should be downloaded
// const destFileName = '/local/path/to/file.txt';

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

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

async function downloadByteRange() {
const options = {
destination: destFileName,
start: startByte,
end: endByte,
};

// Downloads the file from the starting byte to the ending byte specified in options
await storage.bucket(bucketName).file(fileName).download(options);

console.log(
`gs://${bucketName}/${fileName} downloaded to ${destFileName} from byte ${startByte} to byte ${endByte}.`
);
}

downloadByteRange();
// [END storage_download_byte_range]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
15 changes: 15 additions & 0 deletions samples/system-test/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const kmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_US;
const filePath = path.join(cwd, 'resources', fileName);
const folderPath = path.join(cwd, 'resources');
const downloadFilePath = path.join(cwd, 'downloaded.txt');
const startByte = 0;
const endByte = 20;

const fileContent = fs.readFileSync(filePath, 'utf-8');

Expand Down Expand Up @@ -202,6 +204,19 @@ describe('file', () => {
fs.statSync(downloadFilePath);
});

it('should download a file using a given byte range', () => {
const output = execSync(
`node downloadByteRange.js ${bucketName} ${fileName} ${startByte} ${endByte} ${downloadFilePath}`
);
assert.match(
output,
new RegExp(
`gs://${bucketName}/${fileName} downloaded to ${downloadFilePath} from byte ${startByte} to byte ${endByte}.`
)
);
fs.statSync(downloadFilePath);
});

it('should move a file', async () => {
const output = execSync(
`node moveFile.js ${bucketName} ${fileName} ${movedFileName}`
Expand Down
21 changes: 21 additions & 0 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,9 @@ describe('storage', () => {
});

describe('write, read, and remove files', () => {
const FILE_DOWNLOAD_START_BYTE = 0;
const FILE_DOWNLOAD_END_BYTE = 20;

before(async () => {
function setHash(filesKey: string) {
const file = FILES[filesKey];
Expand Down Expand Up @@ -2442,6 +2445,24 @@ describe('storage', () => {
});
});

it('should download the specified bytes of a file', done => {
const fileContents = fs.readFileSync(FILES.big.path);
bucket.upload(FILES.big.path, (err: Error | null, file?: File | null) => {
assert.ifError(err);
file!.download(
{start: FILE_DOWNLOAD_START_BYTE, end: FILE_DOWNLOAD_END_BYTE},
(err, remoteContents) => {
assert.ifError(err);
assert.strictEqual(
String(fileContents).slice(0, 20),
String(remoteContents)
);
done();
}
);
});
});

it('should handle non-network errors', done => {
const file = bucket.file('hi.jpg');
file.download(err => {
Expand Down

0 comments on commit 257f771

Please sign in to comment.