【发布时间】:2019-12-16 17:25:36
【问题描述】:
我是 node.js 的新手,我正在使用以下代码将 csv 文件从 GCS 存储桶下载到本地文件夹。
// 从 GCS 存储桶下载文件的函数
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')
此代码给出以下错误:
(node:10708) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝。此错误源于在异步内部抛出 在没有 catch 块的情况下运行,或者通过拒绝一个承诺 不使用 .catch() 处理。 (拒绝 ID:1)(节点:10708)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。
我需要用不同的方式重写函数吗?
【问题讨论】:
-
downloadFile的调用者应该.catch它可能产生的错误 -
你可以为你建议的更改显示一些示例代码吗?
标签: javascript node.js