AWS:Boto3配置存储桶生命周期 - 格式错误的XML

时间:2017-05-24 13:02:18

标签: amazon-web-services amazon-s3 boto3

以下代码应启用存储桶/存储桶列表的版本控制,然后设置lifecycle configuration

import boto3

# Create session
s3 = boto3.resource('s3')
s3Client = boto3.client('s3')

# Bucket list
buckets = ['BUCKETNAMEHERE']

# iterate through list of buckets
for bucket in buckets:
    # Enable Versioning
    bucketVersioning = s3.BucketVersioning(bucket)
    bucketVersioning.enable()

    # Configure Lifecycle
    s3Client.put_bucket_lifecycle_configuration(
        Bucket=bucket,
        LifecycleConfiguration={
            'Rules': [
                {
                    'Status': 'Enabled',
                    'NoncurrentVersionTransitions': [
                        {
                            'NoncurrentDays': 7,
                            'StorageClass': 'GLACIER'
                        },
                    ],
                    'NoncurrentVersionExpiration': {
                        'NoncurrentDays': 30
                    }
                },
            ]
        }
    )

print "Versioning and lifecycle have been enabled for buckets."

但是,每当我运行此操作时,我都会收到以下错误:

  File "putVersioning.py", line 42, in <module>
    'NoncurrentDays': 30
  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 (MalformedXML) when calling the PutBucketLifecycleConfiguration operation: The XML you provided was not well-formed or did not validate against our published schema

据我所知,一切看起来都正确吗?

1 个答案:

答案 0 :(得分:4)

根据文档here,您需要添加根据Amazon API所需的Filter元素,并且令人困惑,boto不需要。我添加了不推荐使用的Prefix参数而不是Filter,它似乎也在起作用。