Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 1854
春和景丽
春和景丽 2020-11-22 01:19

After uploading a file in Firebase Storage with Functions for Firebase, I\'d like to get the download url of the file.

I have this :

...

return buck         


        
23条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 02:10

    Sorry but i can't post a comment to your question above because of missing reputation, so I will include it in this answer.

    Do as stated above by generating a signed Url, but instead of using the service-account.json I think you have to use the serviceAccountKey.json which you can generate at (replace YOURPROJECTID accordingly)

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

    Example:

    const gcs = require('@google-cloud/storage')({keyFilename: 'serviceAccountKey.json'});
    // ...
    const bucket = gcs.bucket(bucket);
    // ...
    return bucket.upload(tempLocalFile, {
            destination: filePath,
            metadata: {
              contentType: 'image/jpeg'
            }
          })
          .then((data) => {
            let file = data[0]
            file.getSignedUrl({
              action: 'read',
              expires: '03-17-2025'
            }, function(err, url) {
              if (err) {
                console.error(err);
                return;
              }
    
              // handle url 
            })
    

提交回复
热议问题