Search code examples
javascriptnode.jsfirebasegoogle-cloud-platformgoogle-cloud-storage

TypeError: storageRef.getDownloadURL is not a function


exports.add = async (req, res) => {
  const { body } = req;
  var storageRef = fb.firebaseAdmin
    .storage()
    .bucket(
      "gs://test-ptoject-2147f.appspot.com); var filename='computer.jpg'; var path='./computer.jpg'"
    );

  try {
    if (filename == undefined) {
      return res.status(400).send({ message: 'Please upload a file!' });
    }

    const storage = await storageRef.upload(path, {
      public: true,
      destination: `/uploads/${filename}`,
      metadata: {
        firebaseStorageDownloadTokens: uuidv4(),
      },
    });
    res.status(200).send({ message: 'File uploaded successfully.!' });

    storageRef.getDownloadURL().then(function (url) {
      const image = doc('computer');
      image.src = url;
      console.log('your url is:', url);
    });
  } catch (err) {
    console.log(err);
  }
};

Solution

  • With the Admin SDK, with the following code

    var storageRef = fb.firebaseAdmin
        .storage()
        .bucket(...);
    

    you actually define a Bucket and there isn't any getDownloadURL() method for a Bucket.

    You should call the getSignedUrl() method on a File. The getDownloadURL() method is only for the JavaScript SDK.

    The following should do the trick (untested):

    const storage = await storageRef.upload(path, {
      public: true,
      destination: `/uploads/${filename}`,
      metadata: {
        firebaseStorageDownloadTokens: uuidv4(),
      },
    });
    
    const signedUrlResponse = await storageRef.getSignedUrl();
    const url = await signedUrlResponse[0];
    

    Note that it seems there is a typo/problem in this part of your code, with the value you pass to the bucket() method:

      var storageRef = fb.firebaseAdmin
        .storage()
        .bucket(
          "gs://test-ptoject-2147f.appspot.com); var filename='computer.jpg'; var path='./computer.jpg'"
        );