Firebase Storage/Bucket public 使用 node.js 上传

Firebase Storage/Bucket public upload with node.js

代码:

var storage = require('@google-cloud/storage');
var gcs = storage({
    projectId: config.google.projectId,
    keyFilename: config.google.keyFilenameFirebase
});

var bucket = gcs.bucket('project-id.appspot.com');
var destination = 'uploads/12345/full.jpg';

bucket.upload(myFile, { public: true, destination: destination }, function(err, file) {
    if (err) {
        console.log(err);
    }
});

我的文件已成功上传到我的 firebase 存储,但是:

  1. 该文件现在 public 只能通过 url 访问: https://storage.googleapis.com/project-id.appspot.com/uploads/12345/full.jpg。我可以在我的应用程序中使用这个固定的 URL 还是它可能会更改或过期?
  2. public: true 似乎破坏了 Firebase 存储 UI:

右侧的图像预览未显示,我无法通过下载按钮下载图像。此外,没有下载 url(我不介意,因为我可以通过上面的 link 访问它)并且单击 "Create new download URL" 时 Firebase 会产生

Error generating download URL

删除 public: true 时预览会正确显示,我还可以生成下载 URL。但是 public url https://storage.googleapis.com/project-id.appspot.com/uploads/12345/full.jpg 将不再起作用,这对我来说是必需的。

所以我的计划是坚持使用 public: true,但它仍然困扰着我,preview/download 按钮不起作用,对我来说它看起来像是 Firebase 中的一个错误。谁能确认一下?

您的 URL 不应更改或过期,并且它们应该有效直到被撤销,这可以通过对象更新或通过在 firebase 网络控制台中明确撤销 url 来实现。至少据我所知。

我首先想到的是:您是否在存储桶上配置了 CORS?这可能是原因之一。

我能想到的第二件事是存储规则。如果您有类似 allow read: if request.auth.uid == userId; 的内容,您可能想要删除它们以使文件 public 易于访问。您可以使用类似这样的方法在路径 public:

中创建所有文件
 service firebase.storage {
  match /b/XXXXXX.appspot.com/o {
    match /{allPaths=**} {
      allow read;
    }
  }

如果您已经完成了这两件事,那么不幸的是,它实际上也可能是一个 firebase 错误。祝你好运!

1)只要一直public就不会过期。 (我在底部加了一个和这个相关的link)

2) 这是快速修复。 (您可以执行以下任一操作)

  • 在默认ACL中添加自己(以下是使用gsutil的命令)

gsutil defacl ch -u your_email@mail.com:OWNER gs://your_bucket;

  • 在 Google IAM 管理控制台中将自己添加为存储管理员

然后重新上传您的文件....

TL;DR

根据Firebase Documentation,原因如下:

You can use the Google Cloud Storage APIs to access files uploaded via Firebase SDKs for Cloud Storage, especially to perform more complex operations, such as copying or moving a file, or listing all the files available at a reference.

It's important to note that these requests use Google Cloud Storage ACLs or Project Level IAM, rather than Firebase Authentication and Storage Security Rules.

关于public:true,我认为它只会启用和创建public link。参考 here