【问题标题】:How to manage permissions to an object in Google Cloud Storage (Node.js)?如何管理对 Google Cloud Storage (Node.js) 中对象的权限?
【发布时间】:2021-03-23 23:42:54
【问题描述】:

我正在使用谷歌云存储 node.js 客户端 sdk,我想为从 Node.js 上传的对象设置权限。

例如,我希望只有经过身份验证的用户(我自己管理身份验证/授权)能够访问某些文件,而其他文件是公开可用的,但不知道如何实现。

这就是我想要实现的目标

const myBucket = storage.bucket('files_bucket')
const fileRef = myBucket.file('some_file.pdf')

// Here I authenticate the user
const [isAuthenticated, user] = await authenticate(username, password)

if (!isAuthenticated) {
  // The only method I found of making file public, doesnt seem to work
  await fileRef.makePublic()
} else {
  const userRole = user.role // either 'admin' or 'client'

  // How can I make this file private to all users with certain role
  await fileRef.makePrivate()
}

顺便说一句,makePublic 似乎没有公开文件,因为一旦我尝试访问它的 URL,我就会得到

{
  "error": {
    "code": 401,
    "message": "Anonymous caller does not have storage.objects.getIamPolicy access to the Google Cloud Storage object.",
    "errors": [
      {
        "message": "Anonymous caller does not have storage.objects.getIamPolicy access to the Google Cloud Storage object.",
        "domain": "global",
        "reason": "required",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}

【问题讨论】:

  • I manage authentication/authorization myself 是什么意思?你没有使用Cloud Storage Authentication?由于对于 Cloud Storage,您的用户似乎是 Anonymous caller,这似乎是正在发生的事情,我建议您阅读上面的文档和 this one 以公开数据。
  • 我没有使用云存储身份验证或 Google OAuth2 服务。我只是通过用户名和密码对用户进行身份验证,每个人都有一个角色(管理员、客户、卖家......)。有没有办法让所有具有“管理员”角色的用户都可以访问存储桶中的文件?

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


【解决方案1】:

如 cmets 中所述,您没有使用 Cloud Storage Authentication 或 Google OAuth2 服务,因此您的用户在 Cloud Storage 中显示为 Anonymous caller,不允许访问您的资源并且服务被拒绝。

可以在不为您的用户使用 Google 直接身份验证的情况下执行此操作,因此您可以自行管理身份验证/授权,但在尝试访问 GCS 资源(或其他 GCP 产品)时,您将需要模拟作为 GCS 的经过身份验证的调用者出现的服务帐户,此时对特定文件进行自动授权的逻辑将取决于您的代码。

要通过 GCS 库的服务帐户设置访问权限,您可以使用 these instructions in the documentation

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多