簡體   English   中英

如何在 Google Cloud Storage (Node.js) 中管理對 object 的權限?

[英]How to manage permissions to an object in Google Cloud Storage (Node.js)?

我正在使用谷歌雲存儲 node.js 客戶端 sdk,並且我想為從 Z3B2819DD4C24EDA55EEF204 上傳的 object 設置權限。

例如,我只希望經過身份驗證的用戶(我自己管理身份驗證/授權)能夠訪問某些文件,以及其他公開可用的文件,但不知道如何實現這一點。

這就是我想要實現的

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"
      }
    ]
  }
}

正如評論中提到的,您沒有使用雲存儲身份驗證或 Google OAuth2 服務,因此您的用戶顯示為雲存儲的Anonymous caller ,不允許訪問您的資源並且服務被拒絕。

可以在不為您的用戶使用 Google 直接身份驗證的情況下執行此操作,因此您可以自行管理身份驗證/授權,但是在嘗試訪問 GCS 資源(或其他 GCP 產品)時,您將需要模擬一個服務帳戶要顯示為 GCS 的經過身份驗證的調用者,此時對特定文件進行自動化的邏輯將取決於您的代碼。

為了通過 GCS 庫的服務帳戶設置訪問權限,您可以使用文檔中的這些說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM