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

fly*_*s34 3 amazon-s3 amazon-web-services boto3

以下代码应在存储桶/存储桶列表上启用版本控制,然后设置生命周期配置

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."
Run Code Online (Sandbox Code Playgroud)

但是,每当我运行此命令时,都会出现以下错误:

  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
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*zyk 6

根据此处的文档您需要添加Filter元素,这是Amazon API所必需的,而且令人困惑,boto并不需要。我添加了不推荐使用的Prefix参数而不是Filter,它似乎也可以正常工作。