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

Google cloud storage Firebase存储公共URL访问被拒绝(我需要具有公共访问权限的永久URL)makePublic(),google-cloud-storage,firebase-storage,Google Cloud Storage,Firebase Storage,如果在撤销访问令牌时处理不当,GetdownloadURL()将无效。因此,我需要一个没有访问令牌的公共访问永久URL如何makePublic()出现了错误,但我无法断定,gcs.bucket(“Project-12345.appspot.com”).file(fileName.makePublic() 安全规则 service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow r

如果在撤销访问令牌时处理不当,GetdownloadURL()将无效。因此,我需要一个没有访问令牌的公共访问永久URL如何
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;
     }
我得到的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
函数中的
wait
一起使用,如:

const GetUrl = async (productId) => { 
...
   await storageref.makePublic();
...
或者使用
然后
像:

storageref.makePublic().then(() => url[Index] = storageref.publicUrl());
或者用回调


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

感谢您帮助解决我的问题。您剪切的文档[link][]对我很有帮助。现在调用mackPublic()时,我注意到一些错误,如“UnhandledPromisejectionWarning:TypeError[ERR\u INVALID\u ARG\u TYPE]:“path”参数必须是string类型。收到了Object的实例。我已尝试更新
运行脚本
,删除并重新安装节点。但我仍然有相同的错误,如果有不同的错误,你可以发布新的问题和新版本的代码。