Firebase Admin SDK,用于下载/检索Google Cloud Storage上的文件 [英] Firebase Admin SDK to download/retrieve files on Google Cloud Storage

查看:102
本文介绍了Firebase Admin SDK,用于下载/检索Google Cloud Storage上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载一些已上传到Google Cloud Storage的图像(也称为存储桶).我无法在任何const storageconst bucket上使用.ref()方法,因为它们是管理SDK的一部分. admin.storage仅具有方法.bucket()( https ://firebase.google.com/docs/reference/admin/node/admin.storage.Storage ).

I'm trying to download some images that I have uploaded to my Google Cloud Storage (aka into buckets). I'm unable to use the .ref() method on any of my const storage or const bucket because they are part of the admin SDK. The admin.storage has only the method .bucket() (https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage).

我可以访问这些存储桶. bucket.getFiles()起作用,结果是带有大量元数据(例如文件名,存储桶所有者等)的文件数组对象.如何从云存储中获取图像并将其插入html对象?

I'm able to access the buckets. bucket.getFiles() works, and the result comes out to be an Array of Files objects with a ton of metadata (like file name, bucket owner, etc.). How can I get my images from the cloud storage and insert them into html objects?

var admin = require("firebase-admin");

var serviceAccount = require("./randomDB-f12d3-admin-correctly-working.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://randomDB-f12d3.firebaseio.com",
  storageBucket: "randomDB-f12d3.appspot.com"
});

const gcs  = require("@google-cloud/storage");
gcs.projectId = "randomDB-f12d3";
gcs.keyFilename = "randomDB-f12d3-firebase-admin-correctly-working.json";

exports.getFile = functions.https.onRequest((req, res) => {
  
  cors(req, res, () => {
    if (req.method !== "GET") {
      return res.status(500).json({
        message: "Not allowed"
      });
    }

    const storage = admin.storage();
    const bucket = admin.storage().bucket();

    bucket.getFiles().then((result)=>{
      
      console.log(result);
      res.send(result);
    });

  });

});

推荐答案

用于Cloud Storage的Admin SDK只是该库中的存储对象.使用admin.storage().bucket(),您将获得默认存储存储桶用于您的项目.在此处,您应该使用 file() 创建对该存储桶中文件的引用,并根据需要下载它们.

The Admin SDK for Cloud Storage is just a wrapper around the @google-cloud/storage module. When you call admin.storage(), what you're getting back is a Storage object from that library. With admin.storage().bucket(), you're getting the default storage Bucket for your project. From there, you should use file() to create a reference to files in that bucket and download them as needed.

这篇关于Firebase Admin SDK,用于下载/检索Google Cloud Storage上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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