【问题标题】:AWS: Boto3 configuring bucket lifecycle - Malformed XMLAWS:Boto3 配置存储桶生命周期 - 格式错误的 XML
【发布时间】:2021-08-20 19:50:52
【问题描述】:

以下代码应在存储桶/存储桶列表上启用版本控制,然后设置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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 哈!谢谢你。我很确定我的语法是正确的。感谢您的帮助!
    【解决方案2】:

    这个对我有用:

            client.put_bucket_lifecycle_configuration(
                Bucket=s3_bucket,
                LifecycleConfiguration={
                    'Rules': [
                        {
                            'Expiration': {'Days': 5},
                            'Filter': {'Prefix': 'folder1/'},
                            'ID': 'id',
                            'Status': 'Enabled'
                        }
                    ]
                })
    

    要查看实际的 Schema,请在 S3 中创建新规则,然后使用 client.get_bucket_lifecycle_configuration(Bucket=s3_bucket)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2020-04-12
      • 1970-01-01
      相关资源
      最近更新 更多