当前位置: 首页 > 知识库问答 >
问题:

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

沈翰
2023-03-14

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

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

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

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

我试图限制对此文件夹的访问,但用户必须有权列出bucket的根。如果我赋予访问根文件夹的额外权限,用户可以浏览“toto1”和“toto2”文件夹,我不想要。

我想这样配置:

  • 授权列出我的S3帐户的所有存储桶(listAllBuckets策略)
  • 自动调整大小以列出bucket的根目录(如果用户看到目录名,我没问题)
  • 拒绝访问与“toto3”不同的所有前缀bucket
  • 自动调整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": [
                "*"
            ]
        }
    ]
}

共有3个答案

宋宏毅
2023-03-14

我需要只授予外部供应商对文件夹下的子文件夹的访问权限,该文件夹位于bucket(!!!)下。类似这样:

存储桶/文件夹/子文件夹

以下是我是如何做到这一点的:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket",
                "s3:DeleteObject",
                "s3:GetBucketLocation"
            ],
            "Resource": [
                "arn:aws:s3:::bucket/*",
                "arn:aws:s3:::bucket"
            ]
            "Condition": {
                "StringLike": {
                    "s3:prefix": [
                        "bucket/folder/subfolder/*",
                        "bucket/folder/subfolder/",
                        "bucket/folder/subfolder",
                        "folder/subfolder/*",
                        "folder/subfolder/",
                        "folder/subfolder",
                    ]
                }
            }
        }
    ]
}
谷梁鸣
2023-03-14

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

 {
    "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*"
                    ]
                }
            }
        }
    ]
}
谯德元
2023-03-14

以下是一项适用于您的政策:

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

详情:

  • 控制台需要ListAllMyBucket。它显示了所有存储桶的列表
  • 在toto3路径内允许的任何操作
  • ListBucket(检索对象列表)允许在bucket的根和toto3路径中

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

AWS文档参考:允许用户访问Amazon S3中的个人“主页”

 类似资料: