简体   繁体   English

通过云功能检索Firebase存储图像链接

[英]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.

相关问题 使用Firebase Cloud功能检查存储中是否存在映像 - Check if image exists at storage with firebase cloud function 无法使用云删除 Firebase 存储映像 Function - Unable to delete Firebase Storage Image with Cloud Function 从可调用的 https 云 function 将图像上传到 Firebase 存储 - Upload an image into Firebase Storage from a callable https cloud function 从 Firebase 云函数检索响应时出错 - Error in retrieving response from firebase cloud function 使用Google Cloud AutoML模型预测Firebase功能存储在Google Cloud存储中的图像 - Use Google Cloud AutoML model predict an image which is stored in Google Cloud storage in Firebase function 我可以通过 Firebase Cloud Functions 压缩 Firebase 存储中的文件吗? - Can I zip files in Firebase Storage via Firebase Cloud Functions? 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function firebase 存储上上传的缺少图像(使用云 function 更新 Firestore DB) - Missing image uploaded on firebase storage (Using cloud function to update firestore DB) 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage Cloud Storage for Firebase 访问错误“admin.storage(...).ref is not a function” - Cloud Storage for Firebase access error “admin.storage(…).ref is not a function”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM