【发布时间】:2017-05-24 21:03:13
【问题描述】:
我正在尝试向 boto3 传递存储桶名称列表,并让它首先在每个存储桶上启用版本控制,然后在每个存储桶上启用生命周期策略。
我已经完成了 aws configure,并且确实有两个配置文件,都是具有所有必要权限的当前活动用户配置文件。我要使用的名为“default”。
import boto3
# Create session
s3 = boto3.resource('s3')
# Bucket list
buckets = ['BUCKET-NAME']
# iterate through list of buckets
for bucket in buckets:
# Enable Versioning
bucketVersioning = s3.BucketVersioning('bucket')
bucketVersioning.enable()
# Current lifecycle configuration
lifecycleConfig = s3.BucketLifecycle(bucket)
lifecycleConfig.add_rule={
'Rules': [
{
'Status': 'Enabled',
'NoncurrentVersionTransition': {
'NoncurrentDays': 7,
'StorageClass': 'GLACIER'
},
'NoncurrentVersionExpiration': {
'NoncurrentDays': 30
}
}
]
}
# Configure Lifecycle
bucket.configure_lifecycle(lifecycleConfig)
print "Versioning and lifecycle have been enabled for buckets."
当我运行它时,我收到以下错误:
Traceback (most recent call last):
File "putVersioning.py", line 27, in <module>
bucketVersioning.enable()
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/home/user/.local/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/user/.local/lib/python2.7/site-packages/botocore/client.py", line 557, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutBucketVersioning operation: Access Denied
我的个人资料拥有完全权限,所以这应该不是问题。我还需要为passing credentials 做些什么吗?谢谢大家!
【问题讨论】:
-
你写的“profile”是指“role”还是“user”? “默认”获得了哪些特权?如果是角色,您是否验证过您正在机器上运行?如果是用户,您是否验证过您的
AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY有效且处于活动状态? -
抱歉,我没有具体说明。我的个人资料是我的“用户”个人资料。它有效且活跃,我可以通过 aws cli 完成所有操作。
-
这是您的实际代码吗?您的意思是使用名为
'bucket'的存储桶吗? -
这是我的实际代码减去那个。我更改了存储桶名称。
标签: amazon-web-services amazon-s3 boto3