繁体   English   中英

我想将云 function 中的图像保存到 Firestore 并获取 URL

[英]I want to save the image from the cloud function to the firestore and get the URL

我想在云函数(TypeScript)上实现以下过程。

  1. 通过 URL 获取图像
  2. 将图像保存到云存储
  3. 获取firestore中保存的图像的URL

我正在尝试使用以下代码移动它,有 2 个问题。

  • 保存数据需要很长时间(大约 5 分钟)
  • URL 不发给保存的图像

代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM