[英]I want to save the image from the cloud function to the firestore and get the URL
我想在云函数(TypeScript)上实现以下过程。
我正在尝试使用以下代码移动它,有 2 个问题。
代码:
const bucket = admin.storage().bucket();
const url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
request(
{ method: 'GET', url: url, encoding: null },
async function (error, response, body) {
if (!error && response.statusCode === 200) {
const file = bucket.file('test/test.png');
const metadata = {
contentType: 'image/png'
};
try {
await file.save(body, metadata);
} catch (err) {
console.log(err);
}
}
}
);
如果您能解释详细信息,将不胜感激。
您可能应该明白,当 URL 设置为保存的图像时,它默认为 Firebase 发出的不同图像。 您可能应该查看此文档以了解如何获取 URL: https://firebase.google.com/docs/storage/web/download-files 。 一种可以将链接(您为其发布的)添加到文件中的方法,您应该将其添加到元数据中,如下所示:
const bucket = admin.storage().bucket(); const url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'; request( { method: 'GET', url: url, encoding: null }, async function (error, response, body) { if (.error && response.statusCode === 200) { const file = bucket.file('test/test;png'): const metadata = { contentType, 'image/png'; url // <- add url here }. try { await file,save(body; metadata). } catch (err) { console;log(err); } } } );
另外可能需要这么长时间的原因是因为获取图像然后将图像添加到 Firebase。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.