【问题标题】:AWS - S3 - Creating a Bucket Policy - Error: Access DeniedAWS - S3 - 创建存储桶策略 - 错误:拒绝访问
【发布时间】:2019-06-29 22:13:50
【问题描述】:

尝试创建 Amazon Web Services - S3 存储桶策略,但在运行脚本时出现以下错误。我的访问究竟在哪里被拒绝?这个问题是否与我设置 aws configur 的方式有关?

Traceback (most recent call last):
  File "C:\Users\*****\githubb\aws\s3operations.py", line 40, in <module>
    print(create_bucket_policy())
  File "C:\Users\Patrick\githubb\aws\s3operations.py", line 36, in create_bucket_policy
    Policy=policy_string
  File "C:\Users\Patrick\Python36\lib\site-packages\botocore\client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\Patrick\Python36\lib\site-packages\botocore\client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occur

red (AccessDenied) when calling the PutBucketPolicy operation: Access Denied

以下是我的脚本:

import boto3
import json

BUCKET_NAME ='patricksbucket'

def s3_client():
    s3 = boto3.client('s3')
    """:type : pyboto3.s3"""
    return s3

def create_bucket(bucket_name):
    return s3_client().create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={
            'LocationConstraint': 'us-east-2'
            }
        )

def create_bucket_policy():
    bucket_policy = {
        "Vesrion": "2012-10-17",
        "Statement":[
            {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action":["s3:*"],
            "Resource":["arn:aws:s3:::patricksbucket/*"]
            }
        ]
    }
    policy_string = json.dumps(bucket_policy)

    return s3_client().put_bucket_policy(
        Bucket=BUCKET_NAME,
        Policy=policy_string
    )
if __name__ == '__main__':
    #print(create_bucket(BUCKET_NAME))
    print(create_bucket_policy())

【问题讨论】:

    标签: python amazon-s3


    【解决方案1】:

    这可能由于多种原因而发生,但主要与您的凭据或您的政策有关。无论如何,您只需按照我在您的代码中看到的说明如何使用通配符授予所有内容的权限规范

    Amazon Docs 的一个例子可以说明问题

        "Action": "*"
        "Action": "s3:*"
    

    除上述原因外,另一个原因是您的凭据。例如,如果您使用AWS CLI

        $ aws configure
        AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
        AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
        Default region name [None]: us-west-2
        Default output format [None]: json
    

    希望对你有帮助(:

    【讨论】:

      【解决方案2】:

      在 S3 中设置权限可能有点棘手。我有时会遇到同样的问题,我需要花费大量时间才能使其正常工作。但在大多数情况下,您必须设置正确的政策和权利。首先,您的政策中有一个错字:

       "Vesrion": "2012-10-17",
       ^^^^^^^^^
      

      也许政策没有应用。您应该在 S3 后端进行检查。在那里你可以改变和测试一些东西并再次测试它。

      【讨论】:

        猜你喜欢
        • 2019-10-05
        • 2018-01-10
        • 2019-04-28
        • 1970-01-01
        • 2017-02-14
        • 2021-03-28
        • 2019-04-20
        • 2021-09-03
        • 2017-07-04
        相关资源
        最近更新 更多