【发布时间】: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