node.js - 从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL

标签 node.js firebase firebase-storage google-cloud-functions

使用 Firebase 功能在 Firebase 存储中上传文件后,我想获取文件的下载 url。

我有这个:

...

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

        // Get the download url of file

    });

目标文件有很多参数。甚至一个名为 mediaLink。但是,如果我尝试访问此链接,则会收到此错误:

Anonymous users does not have storage.objects.get access to object ...

谁能告诉我如何获取公共(public)下载网址?

谢谢

最佳答案

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

例子:

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

您需要使用 your service account credentials 初始化 @google-cloud/storage因为应用程序默认凭据是不够的。

更新:现在可以通过 Firebase Admin SDK 访问 Cloud Storage SDK,acts as a wrapper围绕@google-cloud/storage。唯一的方法是:

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

关于node.js - 从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956250/

相关文章:

javascript - 用于 cli 登录的 Firebase

javascript - 测试数据是否存在于 Firebase 中

javascript - 如何删除路径在实时数据库中的文件?

android - 如何通过URL下载图片到内存?

python - Firebase将图像从python上传到firebase-storage

node.js - MongoDB 在保存时消失属性

node.js - 在 NodeJs 中使用 mocha 进行模拟

javascript - 如何知道来自 Node 包管理 (NPM) 的包是否经过测试

javascript - 在react状态下初始化pass firebase值

java - 错误 : Program type already present: com. google.firebase.auth.FirebaseAuthException