Get Download URL from file uploaded with Cloud Functions for Firebase

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

    This is what I currently use, it's simple and it works flawlessly.

    You don't need to do anything with Google Cloud. It works out of the box with Firebase..

    // Save the base64 to storage.
    const file = admin.storage().bucket('url found on the storage part of firebase').file(`profile_photos/${uid}`);
    await file.save(base64Image, {
        metadata: {
          contentType: 'image/jpeg',
        },
        predefinedAcl: 'publicRead'
    });
    const metaData = await file.getMetadata()
    const url = metaData[0].mediaLink
    

    EDIT: Same example, but with upload:

    await bucket.upload(fromFilePath, {destination: toFilePath});
    file = bucket.file(toFilePath);
    metaData = await file.getMetadata()
    const trimUrl = metatata[0].mediaLink
    

    #update: no need to make two different call in upload method to get the metadata:

    let file = await bucket.upload(fromFilePath, {destination: toFilePath});
    const trimUrl = file[0].metatata.mediaLink
    

提交回复
热议问题