gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:21:31 25 4
gpt4 key购买 nike

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

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

更新 : Cloud Storage SDK 现在可以通过 Firebase Admin 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/49263855/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com