【问题标题】:Google Cloud Storage report download no access谷歌云存储报告下载无法访问
【发布时间】:2023-03-11 23:40:01
【问题描述】:

我正在运行节点以从谷歌云存储下载销售报告。 我得到了 credentials.json 文件。现在的问题是每次我运行我的应用程序时,我都会得到“xxxxxxx@gmail.com”没有 storage.objects.get access to the Google Cloud Storage object”。

是的,这封电子邮件没有在谷歌云存储上注册或被授予权限,但它应该单独使用凭据,不是吗? 凭据直接来自谷歌云存储,并包含以下信息: client_secret,project_id,redirect_uri,client_id...

我的示例代码:

// Imports the Google Cloud client library.
const {Storage} = require('@google-cloud/storage');



const projectId = 'xxxxxxxxxxxxxxxxxx'
const key = 'credentials.json'
const bucketName = 'pubsite.......'
const destFileName = './test'
const fileName = 'salesreport_2020.zip'

// Creates a client
const storage = new Storage({projectId, key});

async function downloadFile() {
    const options = {
        destination: destFileName,
    };

    // Downloads the file
    await storage.bucket(bucketName).file(fileName).download(options);

    console.log(
        `gs://${bucketName}/${fileName} downloaded to ${destFileName}.`
    );
}

downloadFile().catch(console.error);

【问题讨论】:

    标签: api oauth-2.0 oauth google-api google-cloud-storage


    【解决方案1】:

    您使用了错误类型的凭据文件。

    您的代码是为使用服务帐户 JSON 密钥文件而编写的。您提到凭据文件包含 client_secret。这意味着您正在尝试使用 OAuth 2.0 客户端 ID。

    查看文件 credentials.json。它应该包含“type”:“service_account”。如果您在文件开头看到 {"installed": 或 {"web": ,那么您的凭据有误。

    Creating and managing service account keys

    另外,您在该行中指定了错误的参数:

    const storage = new Storage({projectId, key});
    

    替换为:

    const storage = new Storage({projectId: projectId, keyFilename: key});
    

    【讨论】:

      【解决方案2】:

      因为您看到的是随机 gmail 地址,这可能意味着存储客户端正在使用 Application default credentials 而不是您想要的。前进的道路有两条:

      1. 采用应用程序默认凭据。删除您传递给 Storage 构造函数的选项,而是将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为您的 json 服务帐户文件。

      2. 修复Storage 构造函数以正确传递凭据。问题可能很简单,因为您需要将完整路径传递给凭证文件(即/a/b/c/credentials.json)。可能存储选项没有被正确处理,请尝试明确

        const storage = new Storage({projectId: 'your-project-id', keyFilename: '/path/to/keyfile.json'});
        

      【讨论】:

        猜你喜欢
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-19
        • 2013-03-17
        相关资源
        最近更新 更多