gpt4 book ai didi

google-cloud-storage - Firebase 存储公共(public) URL 访问被拒绝(我需要一个具有公共(public)访问权限的永久 URL)makePublic()

转载 作者:行者123 更新时间:2023-12-04 03:41:23 26 4
gpt4 key购买 nike

如果处理不当 当访问 token 被撤销时,GetdownloadURL() 将失效。因此,我需要一个没有访问 token 的公共(public)访问永久 URL。 How to makePublic() 确实有错误,但我无法得出结论,gcs.bucket("Project-12345.appspot.com").file(fileName ).makePublic()

安全规则

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}}}

我的代码

    const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require('./ProjectID-12345-firebase-adminsdk-.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://ProjectID-12345.firebaseio.com"
});
const {Storage} = require('@google-cloud/storage');
const gcs = new Storage({
projectId: "projectId-12345",
keyFilename: serviceAccount
});

const GetUrl = (productId) => {
const ImgList = ["ImgA_650x650", "ImgB_650x650", "ImgC_650x650", "ImgD_650x650"] ;
let url = []
ImgList.forEach( (element , Index) =>{
let fileName = "product/"+ productId + "/resize"+ element;
let storageref = gcs.bucket("Project-12345.appspot.com").file(fileName);
storageref.makePublic();
url[Index] = storageref.publicUrl();
})
return url;
}

我得到没有访问 token 的 URL

['https://storage.googleapis.com/Project-12345.appspot.com/product/1zHTmjNc5CV4WBLDLgdV/resizeImgA_650x650','https://storage.googleapis.com/Project-12345.appspot.com/product/1zHTmjNc5CV4WBLDLgdV/resizeImgB_650x650','https://storage.googleapis.com/Project-12345.appspot.com/product/1zHTmjNc5CV4WBLDLgdV/resizeImgC_650x650','https://storage.googleapis.com/Project-12345.appspot.com/product/1zHTmjNc5CV4WBLDLgdV/resizeImgD_650x650']

但问题是 URL 被拒绝访问。它将错误显示为

此 XML 文件似乎没有任何关联的样式信息。文档树如下所示。

<Error>
<Code>AccessDenied</Code>
<Message>Access denied.</Message>
<Details>Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.</Details>
</Error>

最佳答案

问题可能与 makePublic() 是异步的事实有关,并且在呈现的代码中它以同步方式使用,结果 publicUrl() 是同步,在它之前执行。

makePublic() 应该在 async 函数中与 await 一起使用,例如:

const GetUrl = async (productId) => { 
...
await storageref.makePublic();
...

或者用 then 像:

storageref.makePublic().then(() => url[Index] = storageref.publicUrl());

或回调。

当然请将其视为伪代码。我不确定你是否可以只复制粘贴它并且它会起作用,无论如何确定该函数是异步的并且必须以这种方式编码。 documentation 中有很好的示例.

关于google-cloud-storage - Firebase 存储公共(public) URL 访问被拒绝(我需要一个具有公共(public)访问权限的永久 URL)makePublic(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65977353/

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