Get Download URL from file uploaded with Cloud Functions for Firebase

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

    This works if you just need a public file with a simple URL. Note that this may overrule your Firebase storage rules.

    bucket.upload(file, function(err, file) {
        if (!err) {
          //Make the file public
          file.acl.add({
          entity: 'allUsers',
          role: gcs.acl.READER_ROLE
          }, function(err, aclObject) {
              if (!err) {
                  var URL = "https://storage.googleapis.com/[your bucket name]/" + file.id;
                  console.log(URL);
              } else {
                  console.log("Failed to set permissions: " + err);
              }
          });  
        } else {
            console.log("Upload failed: " + err);
        }
    });
    
    0 讨论(0)
  • This is the best I came up. It is redundant, but the only reasonable solution that worked for me.

    await bucket.upload(localFilePath, {destination: uploadPath, public: true});
    const f = await bucket.file(uploadPath)
    const meta = await f.getMetadata()
    console.log(meta[0].mediaLink)
    
    0 讨论(0)
  • 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);
    });
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 01:57

    answer by https://stackoverflow.com/users/269447/laurent works best

    const uploadOptions: UploadOptions = {
        public: true
    };
    
    const bucket = admin.storage().bucket();
    [ffile] = await bucket.upload(oPath, uploadOptions);
    ffile.metadata.mediaLink // this is what you need
    
    0 讨论(0)
  • 2020-11-22 01:59

    If you use the predefined access control lists value of 'publicRead', you can upload the file and access it with a very simple url structure:

    // Upload to GCS
    const opts: UploadOptions = {
      gzip: true,
      destination: dest, // 'someFolder/image.jpg'
      predefinedAcl: 'publicRead',
      public: true
    };
    return bucket.upload(imagePath, opts);
    

    You can then construct the url like so:

    const storageRoot = 'https://storage.googleapis.com/';
    const bucketName = 'myapp.appspot.com/'; // CHANGE TO YOUR BUCKET NAME
    const downloadUrl = storageRoot + bucketName + encodeURIComponent(dest);
    
    0 讨论(0)
提交回复
热议问题