【问题标题】:boto3 s3 Object expiration "MalformedXML" errorboto3 s3 对象过期“MalformedXML”错误
【发布时间】:2022-03-10 21:41:35
【问题描述】:

我正在尝试使用boto3put_bucket_lifecycle_configuration 设置 Amazon S3 存储桶中子目录的生命周期配置。我使用来自aws documentation 的这段代码作为参考:

lifecycle_config_settings = {
    'Rules': [
        {'ID': 'S3 Glacier Transition Rule',
         'Filter': {'Prefix': ''},
         'Status': 'Enabled',
         'Transitions': [
             {'Days': 0,
              'StorageClass': 'GLACIER'}
         ]}
    ]}

我删除了Transitions 并添加了Expiration,以更好地满足我的目的。这是我的代码:

myDirectory = 'table-data/'

lifecycle_config_settings = {
    'Rules': [{
        'ID': 'My rule',
        'Expiration': {
            'Days': 30,
            'ExpiredObjectDeleteMarker': True
        },
        'Filter': {'Prefix': myDirectory},
        'Status': 'Enabled'
     }
]}

s3 = boto3.client('s3')
s3.put_bucket_lifecycle_configuration(
    Bucket=myBucket,
    LifecycleConfiguration=lifecycle_config_settings
)

我收到的错误是:

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

什么可能导致这个错误?

【问题讨论】:

  • 这是什么? 'Filter': {'Prefix': myDirectory} 你的 myDictionary 会出错。
  • 如果我要解决这个问题,我会手动创建所需的策略,使用get_bucket_lifecycle_configuration() 检索它,转储检索到的数据结构,看看它们有何不同。
  • @Lamanus myDirectory 是一个具有前缀的变量,我正在尝试应用生命周期规则。在这种情况下,myDirectory = 'table-data/'
  • @Michael-sqlbot,感谢您的建议。我会这样做的。

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


【解决方案1】:

我按照@Michael-sqlbot 的建议,找到了它不起作用的原因。

此设置中的问题在于'ExpiredObjectDeleteMarker': True 中的Expiration key。在boto3 documentation 中有一个关于它的观察。

'ExpiredObjectDeleteMarker' 不能与生命周期过期策略中的天数或日期一起指定。

修复它,设置将是:

lifecycle_config_settings = {
    'Rules': [{
        'ID': 'My rule',
        'Expiration': {
            'Days': 30
        },
        'Filter': {'Prefix': myDirectory},
        'Status': 'Enabled'
     }
]}

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2020-03-27
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多