使用 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。唯一的方法是:
- 使用特殊服务帐户初始化 SDK,通常通过第二个非默认实例。
- 或者,在没有服务帐户的情况下,为默认 App Engine 服务帐户授予“signBlob”权限。
关于node.js - 从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956250/