【问题标题】:Firestore upload and resize image and get the compressed image download urlFirestore 上传并调整图片大小并获取压缩图片下载 url
【发布时间】:2021-07-28 05:49:39
【问题描述】:

我安装了 Resize Images 扩展程序并且它正在工作。 在我的应用中,我有:

const storageRef = firebase.storage().ref();
const imagesRef = storageRef.child(`users/${user?.id}/images`);
const imageRef = imagesRef.child(`${timestamp}.jpg`);
imageRef.put(blob).then((snapshot) => {
  snapshot.ref
    .getDownloadURL()
    .then((image_url) => {

返回的image_url 是上传的原始图片,而不是调整大小的图片。

如何获取调整后图片的下载地址?

我尝试将此添加到响应中:

imagesRef
  .child(`${timestamp}_1000x1000.jpg`)
  .getDownloadURL()
  .then((resized_image_url) => {
    console.log('resized_image_url', resized_image_url);
  });

但它当然不能工作,因为我们不知道压缩图像何时准备好。 显然,在我得到成功响应之前进行某种延迟循环是浪费的。

我在想的一件事(但不喜欢作为一种解决方法)是,由于我在成功调整大小时删除了原始图像,也许我可以以某种方式收听它,当删除时,按照我上面的建议获取调整大小的图像?

那我该怎么办?

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-extensions


    【解决方案1】:

    您需要通过检查是否exists 来测试上传是否完成,这可以在循环或从存储中获取文档引用的超时中完成。

    const storageFile = bucket.file('path/to/compressed/image.jpg');
    storageFile
      .exists()
      .then((exists) => {
            if (exists[0]) {
              console.log("File exists");
            } else {
              console.log("File does not exist");
            }
         })
    

    这是对 firebase 扩展的警告,我发现依靠我们可以调用以在完成时返回值的专用云函数更为合适。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2021-12-09
      • 2017-09-19
      • 2015-09-25
      • 2012-05-28
      • 2015-09-18
      相关资源
      最近更新 更多