我很惊讶地看到这个话题几乎没有在网上出现。
这是我尝试的解决方案:
我一次又一次地遇到错误,因为这似乎非常细微,互联网文档似乎不够用。
我在做什么错?该过程运行了一段时间,然后CF提示“出了点问题...”内存不足。
exports.fetchBilling = async (req, res) => {
const storage = new Storage();
const bucketName = uuid.v4();
await storage.createBucket(bucketName);
//this fetches my data, have confirmed this works
var rows = await dataFrameQuery();
var currAccount = null;
var currFilename = null;
var fileData = "";
var writeStream = null;
for(var i = 0; i < rows.length; i++) {
var row = rows[i];
if(row.ACCOUNT_ID !== currAccount) {
if(currFilename !== null) {
await writeStream.end();
}
currAccount = row.ACCOUNT_ID;
const accName = getBillingName(currAccount);
currFilename = "/tmp/" + i + ".csv";
writeStream = fs.createWriteStream(currFilename);
}
var newLine = row.ACCOUNT_ID +
"," + row.PRODUCT +
"," + row.RESOURCE_TYPE +
"," + row.SKU_ID +
"," + row.MONTH +
"," + row.QUANTITY +
"," + row.UNIT +
"," + row.AMOUNT +
"," + row.CREDIT +
"," + row.CREDIT_DESC
+ "\n";
await writeStream.write(newLine);
if(i === rows.length - 1) {
writeStream.end();
}
}
await zipDirectory("/tmp/", "/tmp/zipped.gzip");
await storage.bucket(bucketName).upload('/tmp/zipped.gzip');
res.send("Done");
};
async function zipDirectory(source, dest) {
const stream = fs.createWriteStream(dest);
const archive = archiver('zip', { zlib: { level: 9 } });
archive.on('error', function(err) {
throw err;
});
await new Promise((resolve, reject) => {
archive.pipe(stream);
archive.directory(source, false);
archive.on('error', err => {throw err;});
archive.finalize();
stream
.on('close', function() {
console.log(`zipped ${archive.pointer()} total bytes.`);
resolve();
});
});
}