我遇到了一个奇怪的问题:
基本上我正在制作Firebase Cloud功能,当有人上传文件时,该文件会通过电子邮件或API发送到其他地方。
一切都适用于小文件(100K-ish),如果速度很慢,但任何超过1MB(未测试确切大小)的都停止。不会在Firebase日志中出现任何错误,该功能永远不会完成。
以下是相关代码:
const Storage = require('@google-cloud/storage')({
projectId: 'bilagskortet',
keyFilename: './service-account-key.json'
});
const returnBase64 = (fileFullPathAndName) => {
console.log("Fetching file...")
// Downloads the file
return Storage
.bucket(bucketName)
.file(fileFullPathAndName)
.download()
.then((data) => {
const file = {};
file.file = new Buffer(data[0]).toString('base64');
return file;
})
.catch((error) => {
console.error("Didn't get file:", error);
});
}
这与其他两个Promise一起使用,以获取电子邮件所需文件的所有内容:
Promise
.all([
StorageFile.returnDownloadURL(attachementRef)
.then(url => {
console.log("AttachmenetLink is: ")
console.log(typeof url);
console.log(url);
email.attachementLink = url
})
.catch(error => {
console.error("Error, didn't get link: ")
console.error(error)
}),
StorageFile.returnMetaData(attachementRef)
.then(metadata => {
console.log("Content type:")
console.log(metadata.contentType);
file.contentType = metadata.contentType;
})
.catch(error => {
console.error("Didn't get the metadata")
console.error(error);
}),
StorageFile.returnBase64(attachementRef)
.then(data => {
console.log("File is: ")
console.log(typeof data);
console.log(data);
file.data = data.file;
})
.catch(error => {
console.error("Error, didn't get file: ")
console.error(error)
})
])
.then(allData => {
// Define and send email with attachement (cut for brevity)
}).catch(error =>
console.error(error)
)
正如我所说,如果文件很小,代码效果很好。如果文件是例如1.7MB图像(.png)
,则超时并死亡任何人都知道可能会发生什么?
记录的最后一件事是“AttachmentLink”和“Content Type”,而StorageFile.returnBase64函数的最后一件事是“获取文件......”