amazon-web-servicesamazon-s3amazon-glaciers3-lifecycle-policy

Malformed XML error while creating the S3 life cycle policy using boto3


I am trying to add a lifecycle rule to a s3 bucket using boto3. But got stuck with following error "Error on adding lifecycle 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". Any idea why I am getting this error?

Here is the rule which i am trying to create

s3 = boto3.resource("s3")
bucket_lifecycle_configuration = s3.BucketLifecycleConfiguration(bucket_name)
date = date.today()


response = bucket_lifecycle_configuration.put(
                LifecycleConfiguration={
                    'Rules': [
                        {
                            'Expiration': {
                                'Date': datetime(date.year, date.month, date.day),
                                 
                                'ExpiredObjectDeleteMarker': True
       
                                
                            },
                            'ID': 'Move unused bucket to Glacier',
                            'Prefix': '',
                            'Status': 'Enabled',
                            'Transitions': [
                                {
                                    'Date': datetime(date.year, date.month, date.day),
                                    'StorageClass': 'GLACIER'
                                }
                            ],
                            'NoncurrentVersionTransitions': [
                                {
                                    'NoncurrentDays': 123,
                                    'StorageClass': 'GLACIER'
                                }
                            ],
                            'NoncurrentVersionExpiration': {
                                'NoncurrentDays': 123
                            },
                            'AbortIncompleteMultipartUpload': {
                                'DaysAfterInitiation': 123
                            }
                        },
                    ]
                }
            )

Solution

  • This is because the ExpiredObjectDeleteMarker flag cannot be used with Dates or Days. thats the cause of the MalformedXML exception. it is explained in the attached reference.

    Also note NoncurrentVersionExpiration date cannot be same as NoncurrentVersionTransitions date. The expiration date should be after the transition date.

    Reference: put_bucket_lifecycle_configuration