[英]Retrieving a Firebase storage image link via Cloud Function
How do I retrieve the download links to stored images in Firebase via a cloud function? 如何通过云功能检索Firebase中存储图像的下载链接?
I've tried all kinds of variations including the next one: 我尝试了各种各样的变化,包括下一个:
exports.getImgs = functions.https.onRequest((req, res) => {
var storage = require('@google-cloud/storage')();
var storageRef = storage.ref;
console.log(storageRef);
storageRef.child('users/user1/avatar.jpg').getDownloadURL().then(function(url) {
});
});
It annoyed me, so I will put the solution with a straight forward explanation to those who are looking for it. 它让我恼火,所以我会给那些正在寻找它的人提供一个直接解释的解决方案。
1st, install GCD Storage using the firebase command line: 1,使用firebase命令行安装GCD Storage:
npm install --save @google-cloud/storage
Cloud function code: 云功能代码:
const gcs = require('@google-cloud/storage')({keyFilename: 'service-account.json'});
const bucket = gcs.bucket('name-of-bucket.appspot.com');
const file = bucket.file('users/user1/avatar.jpg');
return file.getSignedUrl({
action: 'read',
expires: '03-09-2491'
}).then(signedUrls => {
console.log('signed URL', signedUrls[0]); // this will contain the picture's url
});
The name of your bucket can be found in the Firebase console under the Storage section. 您可以在“存储”部分下的Firebase控制台中找到您的存储桶名称。
The 'service-account.json' file can be created and downloaded from here: 可以从此处创建和下载“service-account.json”文件:
https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
And should be stored locally in your Firebase folder under the functions folder. 并且应该存储在函数文件夹下的Firebase文件夹中。 (or other as long as change the path in the code above) (或其他只要更改上面代码中的路径)
That's it. 而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.