【问题标题】:Access Denied on Static Website using Google Platform Bucket使用 Google 平台存储桶在静态网站上拒绝访问
【发布时间】:2018-02-24 03:55:37
【问题描述】:

我正在创建一个存储桶并通过我的 node.js API 将 index.html 文件添加到存储桶中。但是,当我尝试访问该网站时,它给了我这个错误。

AccessDeniedAccess denied.

我做了一些研究,发现 this article 关于运行终端代码以更新 Google Cloud Bucket 上的安全首选项。但是,我的应用程序的性质使我无法在每次创建存储桶时通过终端手动执行此操作。

如何为创建的每个存储桶自动完成此操作?

这是用于创建存储桶的 Node.Js 代码。

exports.createDefaultBucket = functions.https.onRequest((req, res) => {
cors(req, res, () => {

    res.header("Access-Control-Allow-Origin", "*");

    var bucketName = req.body.siteName;

    var defaultIndex = '';
    var defaultCss   = '';

    if(!bucketName) {
        res.send('Bucket name is required!');
    } else {
        storage.createBucket(bucketName).then(() => {
            storage.bucket(bucketName).makePublic().then((response) => {
                console.log(response);
                storage.bucket(bucketName).upload(defaultIndex).then(() => {
                    //storage.bucket(bucketName).upload(defaultCss).then(() => {
                        res.send(bucketName+' was created, made public, has default index file and index location is set!');
                    //})
                })
            }).catch(err => {
                res.send(err);
            })
        }).catch(err => {
            res.send(err);
        });
    }

});
});

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-storage


    【解决方案1】:

    我不确定 makePublic 究竟做了什么,但它可能不会更新 defaultObjectAcl。根据 makePublic 上的文档,您可能需要包含更新文件的选项..但这可能只会更新现有文件。

    https://cloud.google.com/nodejs/docs/reference/storage/1.4.x/Bucket#makePublic

    //-
    // Make the bucket and its contents publicly readable.
    //-
    var opts = {
      includeFiles: true
    };
    
    bucket.makePublic(opts, function(err, files) {
      // `err`:
      //    The first error to occur, otherwise null.
      //
      // `files`:
      //    Array of files successfully made public in the bucket.
    });
    

    我建议改用 IAM。

    https://cloud.google.com/storage/docs/access-control/iam-roles https://cloud.google.com/nodejs/docs/reference/storage/1.4.x/Bucket#iam

    bucket.iam
      .getPolicy()
      .then(results => {
        const policy = results[0];
    
        // Adds the new roles to the bucket's IAM policy
        policy.bindings.push({
          // storage.objectViewers role grants buckets.list and objects.get
          role: 'roles/storage.objectViewers',
          members: 'allUsers',
        });
    
        // Updates the bucket's IAM policy
        return bucket.iam.setPolicy(policy);
      })
    

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 2019-02-03
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多