首页 文章

IAM AWS S3限制到特定的子文件夹

提问于
浏览
5

我正在使用AWS S3组件来存储文件 .

我有一个名为“ mybucket ”的存储桶,其中包含以下文件夹:

+---Mybucket
\---toto1
\---toto2
+---toto3
|   \--- subfolder
|       \---subsubfolder
\---toto4

我有AWS控制台用户,只需要访问“toto3”文件夹 .

我试图限制对此文件夹的访问,但用户必须有权列出存储桶的根目录 . 如果我提供额外的权限来访问根文件夹,用户可以浏览“toto1”和“toto2”文件夹,我不想要 .

我想配置类似的东西:

  • 授权列出我的S3帐户的所有存储桶(listAllBuckets策略)

  • 自动调整以列出存储桶的根目录(如果用户看到目录名称,则对我来说没问题)

  • 拒绝所有前缀桶的访问权限"toto3"

  • 在toto3文件夹中自动调整用户的每个操作

  • 我不想写一个包容性规则

我尝试了这个IAM策略但没有成功:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": ["arn:aws:s3:::mybucket/toto3/*"]
        },
        {
            "Sid": "Stmt1457617383000",
            "Effect": "Allow",
            "Action": [
                "s3:GetBucketLocation",
                "s3:ListBucket"
            ],
            "Resource": ["arn:aws:s3:::mybucket"]
        },
        {
            "Sid": "Stmt1457617230000",
            "Effect": "Deny",
            "Action": ["s3:*"],
            "Condition": {
                "StringNotLike": {
                    "s3:prefix": "toto3*"
                }
            },
            "Resource": [
                "arn:aws:s3:::mybucket/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

2 回答

  • 1

    这是一个适合您的政策:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "s3:ListAllMyBuckets"
                ],
                "Resource": [
                    "*"
                ]
            },
            {
                "Effect": "Allow",
                "Action": [
                    "s3:PutObject",
                    "s3:GetObject"
                ],
                "Resource": [
                    "arn:aws:s3:::mybucket/toto3/*"
                ]
            },
            {
                "Sid": "Stmt1457617230000",
                "Effect": "Allow",
                "Action": [
                    "s3:ListBucket"
                ],
                "Condition": {
                    "StringLike": {
                        "s3:prefix": [
                            "",
                            "toto3/"
                        ]
                    }
                },
                "Resource": [
                    "arn:aws:s3:::mybucket*"
                ]
            }
        ]
    }
    

    细节:

    控制台需要

    • ListAllMyBuckets . 它显示了所有存储桶的列表 .

    • toto3/ 路径中允许的任何操作 .

    • ListBucket (检索对象列表)允许在存储桶的根目录和 toto3/ 路径中 .

    我成功测试了这个解决方案

    AWS文档参考:Allow Users to Access a Personal "Home Directory" in Amazon S3

  • 10

    我编辑你的代码有以下,它的工作原理!谢谢 !!

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": "s3:*",
                "Resource": [
                    "arn:aws:s3:::mybucket/toto3/*"
                ]
            },
            {
                "Effect": "Allow",
                "Action": [
                    "s3:ListAllMyBuckets",
                    "s3:GetBucketLocation"
                ],
                "Resource": "arn:aws:s3:::*"
            },
            {
                "Effect": "Allow",
                "Action": "s3:ListBucket",
                "Resource": "arn:aws:s3:::mybucket",
                "Condition": {
                    "StringLike": {
                        "s3:prefix": [
                            "",
                            "toto3/",
                            "toto3*"
                        ]
                    }
                }
            }
        ]
    }
    

相关问题