简体   繁体   中英

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
                            }
                        },
                    ]
                }
            )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM