|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * This application demonstrates how to perform basic operations on buckets with |
| 19 | + * the Google Cloud Storage API. |
| 20 | + * |
| 21 | + * For more information, see the README.md under /storage and the documentation |
| 22 | + * at https://cloud.google.com/storage/docs. |
| 23 | + */ |
| 24 | + |
| 25 | +const uuid = require('uuid'); |
| 26 | +const path = require('path'); |
| 27 | + |
| 28 | +function main( |
| 29 | + projectId = 'cloud-devrel-public-resources', |
| 30 | + bucketName = `nodejs-storage-samples-${uuid.v4()}`, |
| 31 | + srcFileName = 'test.txt', |
| 32 | + destFileName = path.join(__dirname, `test_${uuid.v4()}.txt`) |
| 33 | +) { |
| 34 | + // [START storage_download_file_requester_pays] |
| 35 | + |
| 36 | + /** |
| 37 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 38 | + */ |
| 39 | + // The project ID to bill |
| 40 | + // const projectId = 'my-billable-project-id'; |
| 41 | + |
| 42 | + // The ID of your GCS bucket |
| 43 | + // const bucketName = 'your-unique-bucket-name'; |
| 44 | + |
| 45 | + // The ID of your GCS file |
| 46 | + // const srcFileName = 'your-file-name'; |
| 47 | + |
| 48 | + // The path to which the file should be downloaded |
| 49 | + // const destFileName = '/local/path/to/file.txt'; |
| 50 | + |
| 51 | + // Imports the Google Cloud client library |
| 52 | + const {Storage} = require('@google-cloud/storage'); |
| 53 | + |
| 54 | + // Creates a client |
| 55 | + const storage = new Storage(); |
| 56 | + |
| 57 | + async function downloadFileUsingRequesterPays() { |
| 58 | + try { |
| 59 | + const options = { |
| 60 | + destination: destFileName, |
| 61 | + userProject: projectId, |
| 62 | + }; |
| 63 | + |
| 64 | + // Downloads the file |
| 65 | + await storage.bucket(bucketName).file(srcFileName).download(options); |
| 66 | + |
| 67 | + console.log( |
| 68 | + `gs://${bucketName}/${srcFileName} downloaded to ${destFileName} using requester-pays requests` |
| 69 | + ); |
| 70 | + } catch (error) { |
| 71 | + console.error( |
| 72 | + 'Error executing download file using requester pays:', |
| 73 | + error.message || error |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + downloadFileUsingRequesterPays(); |
| 79 | + // [END storage_download_file_requester_pays] |
| 80 | +} |
| 81 | +main(...process.argv.slice(2)); |
0 commit comments