Firebase Admin SDK,用于下载/检索谷歌云存储上的文件



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

我可以接近水桶。bucket.getFiles((有效,结果是一个包含大量元数据(如文件名、bucket所有者等(的文件对象数组。如何从云存储中获取图像并将其插入html对象?

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);
});
});
});

云存储管理SDK只是@google Cloud/Storage模块的包装。当您调用admin.storage()时,您得到的是该库中的一个Storage对象。使用admin.storage().bucket(),您将获得项目的默认存储桶。从那里,您应该使用file()来创建对该bucket中文件的引用,并根据需要下载它们。

最新更新