为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Firebase应用程序中获得云存储中所有文件的列表?

如何在Firebase应用程序中获得云存储中所有文件的列表?

白板的微信 2019-06-05 12:43:45
如何在Firebase应用程序中获得云存储中所有文件的列表?我正在上传图片,一切都很好,但我有100张照片,我想在我的View,当我在文件夹中获得图像的完整列表时,我找不到用于这项工作的任何API。
查看完整描述

3 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

加上火基云函数以及Firebase与GoogleCloud的更深层次的集成,这已经成为可能。

使用云函数,您可以使用GoogleCloudNode包来完成史诗云存储操作。下面是一个从CloudStorage获取所有文件URL到数组中的示例。这个功能每次被保存到Google云存储时都会被触发。

附注1这是一个计算上相当昂贵的操作,因为它必须遍历桶/文件夹中的所有文件。

附注2:我写这个只是作为一个例子,没有把很多细节写成承诺等等。只是想给个主意。

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();

// let's trigger this function with a file upload to google cloud storage

exports.fileUploaded = functions.storage.object().onChange(event => {

  const object = event.data; // the object that was just uploaded
  const bucket = gcs.bucket(object.bucket);
  const signedUrlConfig = { action: 'read', expires: '03-17-2025' }; // this is a signed url configuration object

  var fileURLs = []; // array to hold all file urls 

  // this is just for the sake of this example. Ideally you should get the path from the object that is uploaded :)
  const folderPath = "a/path/you/want/its/folder/size/calculated";

  bucket.getFiles({ prefix: folderPath }, function(err, files) {
    // files = array of file objects
    // not the contents of these files, we're not downloading the files. 

    files.forEach(function(file) {
      file.getSignedUrl(signedUrlConfig, function(err, fileURL) {
        console.log(fileURL);
        fileURLs.push(fileURL);
      });
    });

  });

});

我希望这能给你一个大致的想法。要获得更好的云功能示例,请查看Google的Gizubrepo充满了用于FireBase的云功能示例..也检查他们的GoogleCloudNodeAPI文档


查看完整回答
反对 回复 2019-06-05
  • 3 回答
  • 0 关注
  • 682 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信