2017-10-17 101 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

相關問題