簡體   English   中英

AWS Cognito-使用IAM角色的S3訪問

[英]AWS Cognito - S3 Access using IAM role

目前,我正在使用一個用例,在該用例中,我需要使用AWS Cognito對Web應用程序用戶進行身份驗證,並提供對S3存儲桶中用戶特定文件夾的訪問。 為此,我在瀏覽器端使用JavaScript並能夠驗證用戶身份。

我創建了一個IAM策略,在該策略中,我試圖將S3對特定於用戶的文件夾的訪問限制為經過身份驗證的用戶。 不知何故,此政策無效。

我在這里嘗試的政策如下:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "cognito-identity:*",
            "mobileanalytics:PutEvents",
            "cognito-sync:*"
        ],
        "Resource": "*"
    },
    {
        "Sid": "VisualEditor1",
        "Effect": "Allow",
        "Action": "s3:ListBucket",
        "Resource": "arn:aws:s3:::mycognitobuckettest",
        "Condition": {
            "StringLike": {
                "s3:prefix": [
                    "cognito/mycognitobuckettest/${cognito-identity.amazonaws.com:sub}/",
                    "cognito/mycognitobuckettest/${cognito-identity.amazonaws.com:sub}/*"
                ],
                "s3:delimiter": [
                    "/"
                ]
            }
        }
    },
    {
        "Sid": "VisualEditor2",
        "Effect": "Allow",
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::mycognitobuckettest/cognito/mycognitobuckettest/${cognito-identity.amazonaws.com:sub}/*"
    }
]}`

如果從資源路徑中刪除${cognito-identity.amazonaws.com:sub} ,則可以訪問S3存儲桶。 但是,如果我將其放入資源路徑以及條件鍵中,則會出現拒絕訪問錯誤。

根據我的理解, ${cognito-identity.amazonaws.com:sub}的值是區域ID和uuid,您可以像這樣獲得var identityID = AWS.config.credentials.identityId

我正在嘗試列出資源路徑中的對象,下面是我用來列出對象的JavaScript代碼

var listObjectParms = {Bucket: bucketName, Delimiter: '/', Prefix: bucketPrefix};

    s3.listObjects(listObjectParms, function(err, data) {
        if(err) {
            console.log("Error", err);
        } else {
            console.log("LIST OBJECT successful", data);
        }
    });

${cognito-identity.amazonaws.com:sub}的確切值是多少?

我已經根據收到的身份ID在S3存儲桶中創建了文件夾。

我現在被困在這一點上。 歡迎您提供有關解決此問題的任何幫助。

謝謝,

阿維納什

我最近遇到了這個問題。 $ {cognito-identity.amazonaws.com:sub}的值與您的cognito用戶池無關。 這是您從cognto身份獲得的身份ID。

例如:us-east-1:aaaaaaa-bbbb-1111-ccccccccc11f

您可以使用get_id方法在javascript sdk中獲取該ID以進行認知

暫無
暫無

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

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