在 firebase 的调整大小扩展完成后获取下载 URL

Get Download Url after firebase's resize extension completed

提问人:Haniel Baez 提问时间:10/1/2019 最后编辑:Doug StevensonHaniel Baez 更新时间:10/23/2023 访问量:1367

问:

这就是我试图实现的目标,实现 firebase 的调整大小图像扩展,上传图像,然后在调整大小完成后,将该 dowloadUrl 的拇指添加到 Cloud Firestore 文档中。这个问题对我有帮助,但仍然无法识别拇指并获取下载URL,这就是我迄今为止一直在尝试的。

注意:我将缩略图设置为根/拇指

const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();

exports.thumbsUrl = functions.storage.object().onFinalize(async object => {
    const fileBucket = object.bucket;
    const filePath = object.name;
    const contentType = object.contentType;
    if (fileBucket && filePath && contentType) {
        console.log('Complete data');
         if (!contentType.startsWith('thumbs/')) {
             console.log('This is not a thumbnails');
             return true;
         }
         console.log('This is a thumbnails');


    } else {
        console.log('Incomplete data');
        return null;
    }
});
javascript firebase google-cloud-functions firebase-storage

评论

0赞 hugger 10/29/2019
我在使用扩展之前遇到了这个问题,最终使用了 signedURL......问题是 URL 会在大约一周后过期,使缩略图空白且无用......我不知道为什么他们没有在扩展本身上包含这方面的文档。轻松生成缩略图是件好事,但是例如,将缩略图的下载网址存储在RTDT中时呢?我期待着对此有一个可靠的答案!
0赞 Jingles 6/16/2020
你找到解决这个问题的方法了吗?
0赞 MadMac 7/10/2020
继续轮询以查看何时准备就绪的解决方案。这对我来说效果很好:stackoverflow.com/questions/58977241/......
0赞 Abu Saeed 1/14/2022
你有什么解决方案吗?
0赞 Haniel Baez 1/14/2022
不,我不再从事该项目。

答:

-1赞 Siddhant Jaiswal 12/27/2019 #1

您需要使用 filePath 来检查拇指if(filePath.startswith('thumbs/'){...}

contentType 具有文件的元数据,例如图像类型等。 FilePath 将具有完整路径。

0赞 Dr.House 8/11/2022 #2

方法1:客户端

  • 创建缩略图时不要更改。access token
  • 在 gcloud 云函数控制台中编辑函数
  • 通过单击转到函数代码detailed usage stats
  • 然后点击代码
  • 编辑以下行
  • 再次重新部署函数
  // If the original image has a download token, add a
        // new token to the image being resized #323
        if (metadata.metadata.firebaseStorageDownloadTokens) {
            // metadata.metadata.firebaseStorageDownloadTokens = uuidv4_1.uuid();
        }
  • 使用 getDownloadURL函数获取上传的图片
https://firebasestorage.googleapis.com/v0/b/<project_id>/o/<FolderName>%2F<Filename>.jpg?alt=media&token=xxxxxx-xxx-xxx-xxx-xxxxxxxxxxxxx

  • 因为访问令牌将是相似的
https://firebasestorage.googleapis.com/v0/b/<project_id>/o/<FolderName>%2Fthumbnails%2F<Filename>_300x300.jpg?alt=media&token=xxxxxx-xxx-xxx-xxx-xxxxxxxxxxxxx

方法 2:服务器端

创建缩略图后调用此函数

var storage = firebase.storage();
    var pathReference = storage.ref('users/' + userId + '/avatar.jpg');
    pathReference.getDownloadURL().then(function (url) {
        $("#large-avatar").attr('src', url);
    }).catch(function (error) {
        // Handle any errors
    });
0赞 Fábio C. Souza 10/23/2023 #3

我在一些项目中遇到过类似的问题,我找到的解决方案如下:

  1. 定义文件路径。

  2. 根据扩展名配置建立调整大小的文件路径。

  3. 上传图片。

  4. 添加 6 秒延迟以允许扩展工作。

  5. 利用调整大小的图像路径获取下载 URL。

     endCrop(){
    const file:any = this.croppedImage.split(',')[1];
    let filePath = 'shop_img/' + this.auth.User.authid + '/' + 'imgname.png';
    let resizePath = 'shop_img/' + this.auth.User.authid + '/' +'imgname_600x600.png';
    
    this.storage.uploadImage(file, filePath).then(async urlorigem =>{
      await this.delay(6000); //delay of 6s
        this.storage.storage.ref(resizePath).getDownloadURL().subscribe(url =>{
          this.croppedImage = url; //got url from resized image
        })
     })
    

    }

0赞 DAHAOREN 12/1/2023 #4

当 Firebase 的调整大小扩展程序创建缩略图时,它应该将其存储在特定路径中,您提到的该路径设置为 root/thumbs。您编写的函数正在尝试检查对象是否为缩略图,但 contentType.startsWith('thumbs/') 条件看起来关闭。相反,您应该检查 filePath 是否包含“thumbs”目录。类似于 if (filePath.startsWith('thumbs/'))。