I am new to node.js & am using below code to download a csv file from GCS bucket to local folder.
// function to download file from GCS bucket
async function downloadFile(bucketName, srcFilename, destFilename) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client from a Google service account key.
const storage = new Storage({keyFilename: "keyFile.json"});
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'Remote file to download, e.g. file.txt';
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: destFilename,
};
// Downloads the file
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
// [END storage_download_file]
}
// call downloadFile function
downloadFile('temp_bucket123','test_data.csv','./gcs_download/test_data.csv')
This code gives below error:
(node:10708) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10708) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Do i need to re-write the function in a different way?
It appears that something inside of downloadFile()
is creating an error that results in a rejected promise being returned from downloadFile()
. That warning is because you have no error handling for that and unhandled rejections are considered evil. As the error says, in the future they may even automatically terminate your node.js process.
downloadFile('temp_bucket123','test_data.csv','./gcs_download/test_data.csv').then(() => {
console.log("done with download now");
}).catch(err => {
console.log(err);
});
All async functions that have any way to possibly reject their promise must have an error handler on them such as .catch()
or with try/catch
if using await
to call the function.
You can also catch errors at the source and make sure that downloadFile()
can't reject its promise. Either way, you have to catch the error somewhere and not let a rejected promise go unhandled. The designers of both promises and node.js have decided that an unhandled rejected promise should be just about akin to an unhandled exception (since they are logically pretty similar - a rejected promise being an exception for asynchronous code).
For async
operation you should add a fallback if any error
occurs, you can add try/catch
block inside your method
async function downloadFile(bucketName, srcFilename, destFilename) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client from a Google service account key.
const storage = new Storage({keyFilename: "keyFile.json"});
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'Remote file to download, e.g. file.txt';
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: destFilename,
};
// Downloads the file
try {
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
} catch (err) {
console.log('Some error occured', err)
}
// [END storage_download_file]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.