2017-10-17 104 views
1

如何通过云端功能检索Firebase中存储图像的下载链接?通过云端功能检索Firebase存储图像链接

我已经尝试了各种变化,包括下一个:

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) { 

    }); 
}); 
+1

可能出现的[使用Cloud Functions for Firebase上传的文件获取下载URL](https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions- for-firebase) –

回答

1

这惹恼了我,所以我会把解决方案具有直接的解释那些谁是寻找它。

1,使用火力命令行安装GCD存储:

npm install --save @google-cloud/storage 

云功能代码:

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

您的存储分区的名称可以根据存储部分中的火力地堡控制台被发现。

的“服务account.json”文件可以创建和下载从这里:

https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk

而且应在功能文件夹下的文件夹火力地堡的本地存储。 (或其他只要在上面的代码中改变路径)

就是这样。

+0

出于安全原因动态设置url的到期可能是一个好主意。 – Badrush