Get Download URL from file uploaded with Cloud Functions for Firebase

后端 未结 23 1801
春和景丽
春和景丽 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 01:56

    You should avoid harcoding URL prefix in your code, especially when there are alternatives. I suggest using the option predefinedAcl: 'publicRead' when uploading a file with Cloud Storage NodeJS 1.6.x or +:

    const options = {
        destination: yourFileDestination,
        predefinedAcl: 'publicRead'
    };
    
    bucket.upload(attachment, options);
    

    Then, getting the public URL is as simple as:

    bucket.upload(attachment, options).then(result => {
        const file = result[0];
        return file.getMetadata();
    }).then(results => {
        const metadata = results[0];
        console.log('metadata=', metadata.mediaLink);
    }).catch(error => {
        console.error(error);
    });
    

提交回复
热议问题