使用Firebase存储上传后如何获取调整后大小的downloadUrl(Web SDK +调整图像大小) [英] How to get the resized downloadUrl after upload with firebase storage (Web SDK + Resize images extention)

查看:73
本文介绍了使用Firebase存储上传后如何获取调整后大小的downloadUrl(Web SDK +调整图像大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase Web SDK,我可以在上传文件后轻松获得downloadUrl

With the Firebase Web SDK, I can easily get the downloadUrl after upload a file

const task = firebase.ref().child('myFolder').put(file, metadata)
task.then(snapshot => {
 snapshot.ref.getDownloadURL()) 
}

但是我安装了调整图像的大小,现在,我想要以获得大小调整后的downloadUrl.怎么做 ?我找不到任何解释...

But I installed the Resize Images Extention and now, I want to get the resized downloadUrl as soon as it is available. How to do that ? I do not find any explanations about it...

推荐答案

扩展名根据您的配置方式确定新文件名.您可以在扩展名的

The extension deterministicly determines the new file name based on how you configured it. You can see the exact code for how the name is determined in the extension's source code.

在安装扩展程序时,它要求提供一个调整大小后的图像的路径,该路径是相对于原始路径的.那就是新图像的路径(当然是相对于原始图像的路径).

When you installed the extension, it asked for a path to the resized images which is relative to the path of the original. That is the path to the new image (relative, of course, to the original).

除此之外,文档指出它将带有后缀.与配置的宽度和高度.

Beyond that, the documentation states that it will be suffixed with the configured width and height.

使用与原始上载的图像相同的名称命名调整大小的图像,但后缀为您指定的宽度和高度.

Names the resized image using the same name as the original uploaded image, but suffixed with your specified width and height.

因此,如果您未指定路径,并且指定了200x200,然后将image.jpg上传到存储桶的根目录,则新名称将是:image_200x200.jpg,位于存储桶的根目录.

So, if you didn't specify a path, and you specified 200x200, and then uploaded image.jpg to root of the bucket, the new name would be: image_200x200.jpg, at the root of the bucket.

如果您指定路径resized,并且指定了200x200,并且将image2.jpg上载到存储桶的根目录,则新名称将是/resized/image2_200x200.jpg,与源映像位于同一存储桶中.

If you specified the path resized, and you specified 200x200, and the uploaded image2.jpg to the root of the bucket, the new name would be /resized/image2_200x200.jpg in the same bucket as the source image.

要获取下载URL,扩展功能创建新文件后,您需要使用新名称调用存储引用上的getDownloadURL.

To get the download URL, you will need to call getDownloadURL on a storage reference with the new name once the extension function has created the new file.

如果要等待,可以使用类似于以下内容的代码进行轮询:

If you want to wait, you can poll with code similar to the following:

function delay(t, v) {
  return new Promise(function(resolve) { 
    setTimeout(resolve.bind(null, v), t)
  });
}

function keepTrying(triesRemaining, storageRef) {
  if (triesRemaining < 0) {
    return Promise.reject('out of tries');
  }

  return storageRef.getDownloadURL().then((url) => {
    return url;
  }).catch((error) => {
    switch (error.code) {
      case 'storage/object-not-found':
        return delay(2000).then(() => {
          return keepTrying(triesRemaining - 1, storageRef)
        });
      default:
        console.log(error);
        return Promise.reject(error);
    }
  })
}

这就是您在上传后如何称呼它:

And this is how you would call it after the upload:

const storageRef = firebase.storage().ref().child('image_200x200.jpg');
keepTrying(10, storageRef).then((url) => console.log(url));

这篇关于使用Firebase存储上传后如何获取调整后大小的downloadUrl(Web SDK +调整图像大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆