从firebase存储中下载图像,并使用node.js云函数添加到jszip中



几天来,我一直在尝试各种方法,但遇到了困难。我有存储在firebase存储中的图像,我想将其添加到一个zip文件中,该文件通过电子邮件与其他一些表单一起发送。我已经尝试了很多次迭代,但当jpeg文件被添加到输出的zip中时,任何应用程序都无法打开它。

这是我的最新迭代:

exports.sendEmailPacket = functions.https.onRequest(async (request, response) => {
const userId = request.query.userId;
const image = await admin
.storage()
.bucket()
.file(`images/${userId}`)
.download();
const zipped = new JSZip();
zipped.file('my-image.jpg', image, { binary: true });
const content = await zipped.generateAsync({ type: 'nodebuffer' });
// this gets picked up by another cloud function that delivers the email
await admin.firestore()
.collection("emails")
.doc(userId)
.set({
to: 'myemail@gmail.com',
message: {
attachments: [
{
filename: 'test.mctesty.zip',
content: Buffer.from(content)
}
]
}
});
});

经过更多的研究,我们发现了这一点:

exports.sendEmailPacket = functions.https.onRequest(async (request, response) => {
const userId = request.query.userId;
const image = await admin
.storage()
.bucket()
.file(`images/${userId}`)
.get(); // get instead of download
const zipped = new JSZip();
zipped.file('my-image.jpg', image[0].createReadStream(), { binary: true }); // from the 'File' type, call .createReadStream()
const content = await zipped.generateAsync({ type: 'nodebuffer' });
// this gets picked up by another cloud function that delivers the email
await admin.firestore()
.collection("emails")
.doc(userId)
.set({
to: 'myemail@gmail.com',
message: {
attachments: [
{
filename: 'test.mctesty.zip',
content: Buffer.from(content)
}
]
}
});
});

相关内容

  • 没有找到相关文章

最新更新