提问者:小点点

从使用Firebase云功能上传的文件中获取下载URL


在Firebase存储中上传了一个具有Firebase功能的文件后,我想获得该文件的下载url。

我有这个:

...

return bucket
    .upload(fromFilePath, {destination: toFilePath})
    .then((err, file) => {

        // Get the download url of file

    });

对象文件有很多参数。甚至是一个名为medialink。但是,如果尝试访问此链接,则会出现以下错误:

匿名用户没有Storage.Objects.获取对象访问...

有人能告诉我如何获得公共下载URL吗?

谢谢


共1个答案

匿名用户

您需要通过@google-cloud/storage NPM模块使用getSignedURL生成一个签名的URL。

示例:

const gcs = require('@google-cloud/storage')({keyFilename: 'service-account.json'});
// ...
const bucket = gcs.bucket(bucket);
const file = bucket.file(fileName);
return file.getSignedUrl({
  action: 'read',
  expires: '03-09-2491'
}).then(signedUrls => {
  // signedUrls[0] contains the file's public URL
});

您需要使用服务帐户凭据初始化@google-cloud/storage,因为应用程序的默认凭据是不够的。

更新:现在可以通过Firebase Admin SDK访问云存储SDK,它充当@google-cloud/Storage的包装器。唯一的方法是:

  1. 使用特殊服务帐户初始化SDK,通常通过第二个非默认实例。
  2. 或者,在没有服务帐户的情况下,向默认App Engine服务帐户授予“SignBlob”权限。