【发布时间】:2020-05-15 12:37:51
【问题描述】:
我正在尝试从云存储加载 csv 文件,更改标题并将其重新上传到云存储。
// Import the Google Cloud client libraries
const { Storage } = require('@google-cloud/storage');
const lineReplace = require('line-replace');
const storage = new Storage();
const bucketName = 'bucket';
const srcFilename = 'bq/cost.csv';
const localFilename = 'tmp/cost.csv';
const bucketFilename = 'ga/cost.csv';
exports.properGAHeaders = async (data, context) => {
try {
const file = data;
// Downloads the file
if (file.name = srcFilename) {
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: localFilename,
};
await storage.bucket(bucketName).file(srcFilename).download(options);
await lineReplace({
file: localFilename,
line: 1,
text: 'ga:date,ga:medium,ga:source,ga:adClicks,ga:adCost,ga:impressions,ga:referralPath,ga:adContent,ga:campaign,ga:adFinalUrl',
addNewLine: false,
callback: ({
file, line, text, replacedText, error,
}) => {
if (error) { console.log(error); }
console.log(`${replacedText} is replaced with ${text}!`);
},
});
await storage.bucket(bucketName).upload(localFilename, {
destination: bucketFilename,
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: false,
// By setting the option `destination`, you can change the name of the
// object you are uploading to a bucket.
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'no-cache',
},
});
console.log(`${localFilename} uploaded to ${bucketName}`);
}
} catch (err) { console.error(err); }
};
执行此操作时,我收到错误 Error: ENOENT: no such file or directory, open 'tmp/cost.csv'。
可能是什么问题? 我正在使用 await 运行这些函数,因此它们应该同步运行。
【问题讨论】:
标签: node.js google-cloud-functions google-cloud-storage