【问题标题】:I want to save the image from the cloud function to the firestore and get the URL我想将图像从云功能保存到 Firestore 并获取 URL
【发布时间】:2020-09-22 22:14:15
【问题描述】:

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

  1. 通过网址获取图片
  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);
      }
    }
  }
);

如能详细说明,将不胜感激。

【问题讨论】:

标签: node.js firebase google-cloud-firestore google-cloud-functions google-cloud-storage


【解决方案1】:

您可能应该明白,当 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。

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 2021-10-28
    • 2021-03-14
    • 2018-04-18
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多