Firebase的resize扩展完成后如何获取下载链接

30

我的目标是实现Firebase的缩放图像扩展,上传一张图片,然后当调整大小完成后,将该下载URL的缩略图添加到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;
    }
});

我之前使用过这个扩展程序时遇到了这个问题,最终使用了signedURL... 但问题在于URL会在大约一周后过期,导致缩略图变成空白无用的东西... 我不知道为什么他们没有在扩展程序本身中包含这方面的文档。生成缩略图很容易,但是当需要将该缩略图的下载URL存储在RTDT中时怎么办呢?我期待着一个可靠的答案! - hugger
你找到解决方案了吗? - Jingles
保持轮询以查看何时准备就绪的解决方案。这对我很有效:https://dev59.com/PlMH5IYBdhLWcg3wmwTw - MadMac
你有任何解决方案吗? - Abu Saeed
不,我已经不再从事这个项目了。 - Haniel Baez
4个回答

0

方法一:客户端

  • 在创建缩略图时不要更改访问令牌
  • 从gcloud云函数控制台编辑函数。
  • 通过单击详细使用统计信息进入函数代码。
  • 然后单击代码。
  • 编辑以下行。
  • 重新部署该函数。
  // 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

方法二:服务器端

在缩略图创建后调用此函数。

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
当Firebase的resize扩展创建缩略图时,它应该将其存储在特定路径中,您已经提到该路径设置为root/thumbs。您编写的函数试图检查对象是否为缩略图,但是contentType.startsWith('thumbs/')的条件看起来不对。相反,您应该检查filePath是否包含'thumbs'目录。类似这样的if (filePath.startsWith('thumbs/'))。

0
我在一些项目中遇到过类似的问题,我找到的解决方案如下:
  1. 定义文件路径。
  2. 根据扩展配置建立调整大小后的文件路径。
  3. 上传图片。
  4. 添加6秒的延迟以使扩展程序正常工作。
  5. 利用调整大小后的图片路径获取下载URL。
  6.  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); //延迟6秒
        this.storage.storage.ref(resizePath).getDownloadURL().subscribe(url =>{
          this.croppedImage = url; //从调整大小后的图片获取URL
        })
     })
    }

    }


-1

你需要使用filePath来检查缩略图 if(filePath.startswith('thumbs/'){...}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接