简体   繁体   English

Firebase Admin SDK可下载/检索Google Cloud Storage上的文件

[英]Firebase Admin SDK to download/retrieve files on Google Cloud Storage

I'm trying to download some images that I have uploaded to my Google Cloud Storage (aka into buckets). 我正在尝试下载一些已上传到Google Cloud Storage的图像(也称为存储桶)。 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. 我无法在任何const storageconst bucket上使用.ref()方法,因为它们是管理SDK的一部分。 The admin.storage has only the method .bucket() ( https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage ). admin.storage仅具有.bucket()方法( https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage )。

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.). bucket.getFiles()起作用,结果是带有大量元数据(如文件名,存储桶所有者等)的文件数组对象。 How can I get my images from the cloud storage and insert them into html objects? 如何从云存储中获取图像并将其插入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); }); }); }); 

The Admin SDK for Cloud Storage is just a wrapper around the @google-cloud/storage module . 云端存储的管理SDK只是@ google-cloud / storage模块的包装。 When you call admin.storage() , what you're getting back is a Storage object from that library. 当您调用admin.storage() ,您得到的是该库中的一个Storage对象。 With admin.storage().bucket() , you're getting the default storage Bucket for your project. 使用admin.storage().bucket() ,您将获得项目的默认存储 From there, you should use file() to create a reference to files in that bucket and download them as needed. 从那里,您应该使用file()创建对该存储桶中文件的引用,并根据需要下载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用admin SDK将文件上传到带有路径的Firebase的云存储中? - How can I upload files to firebase's cloud storage with a path using the admin sdk? 使用 NodeJS 从 Google Cloud Storage 中的一组路径中检索和下载所有文件 - Retrieve and download all files from a set of paths in Google Cloud Storage using NodeJS 从 Google firebase 存储文件中检索前缀 - Retrieve prefixes from Google firebase storage files 如何将文件从谷歌云存储下载到用户电脑(React) - How to download files from google cloud storage to user pc (React) 无法使用Firebase功能删除Google Cloud Storage文件 - Cannot delete Google Cloud Storage files using Firebase Functions Firebase 管理 SDK 不适用于 Cloud Scheduler (PubSub) - Firebase admin SDK not working on Cloud Scheduler (PubSub) Firebase Cloud Firestore Admin SDK 问题 - Firebase Cloud Firestore Admin SDK issue 重命名谷歌云存储中的文件? - renaming files in Google Cloud Storage? Cloud Storage for Firebase SDK 6.1.0:无法使用listAll()列出存储桶中的文件 - Cloud Storage for Firebase SDK 6.1.0 : can't list files in a bucket using listAll() 如何使用 google-cloud 和 node js 从 firebase 存储中检索图像并将其显示在网页上 - how to retrieve image from firebase storage and display it on a webpage using google-cloud and node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM