AWS: Boto3 configuring bucket lifecycle - Malformed XML

Viewed 2366

The following code should enable versioning on a bucket/list of buckets, and then set the 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."

However, whenever I run this I get the following error:

  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

As far as I can tell, everything looks correct?

2 Answers

This one works for me:

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

To see an actual Schema, create a new rule in S3, and then use client.get_bucket_lifecycle_configuration(Bucket=s3_bucket)

Related