TypeError: storageRef.getDownloadURL is not a function

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);
  }
};

使用 Admin SDK,使用以下代码

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

您实际上定义了一个 Bucket 并且 Bucket 没有任何 getDownloadURL() 方法。

您应该在 File 上调用 getSignedUrl() 方法。 getDownloadURL() 方法仅适用于 JavaScript SDK。

以下应该可以解决问题(未经测试):

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

const signedUrlResponse = await storageRef.getSignedUrl();
const url = await signedUrlResponse[0];

请注意,您的这部分代码中似乎有一个 typo/problem,您传递给 bucket() 方法的值是:

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