Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 1810
春和景丽
春和景丽 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:06

    You'll need to generate a signed URL using getSignedURL via the @google-cloud/storage NPM module.

    Example:

    const gcs = require('@google-cloud/storage')({keyFilename: 'service-account.json'});
    // ...
    const bucket = gcs.bucket(bucket);
    const file = bucket.file(fileName);
    return file.getSignedUrl({
      action: 'read',
      expires: '03-09-2491'
    }).then(signedUrls => {
      // signedUrls[0] contains the file's public URL
    });
    

    You'll need to initialize @google-cloud/storage with your service account credentials as the application default credentials will not be sufficient.

    UPDATE: The Cloud Storage SDK can now be accessed via the Firebase Admin SDK, which acts as a wrapper around @google-cloud/storage. The only way it will is if you either:

    1. Init the SDK with a special service account, typically through a second, non-default instance.
    2. Or, without a service account, by giving the default App Engine service account the "signBlob" permission.

提交回复
热议问题