S3 lifecycle rule / ExpiredObjectDeleteMarker': True failed on chalice application

0

Hi Inside a chalice application, I have created a class for handling S3 bucket. I add a method to add a default lifecycle rule which will delete all objects after 1 day. (quite useful for a dev environment)

def put_default_lifecycle_rule(self, id_name='default_lifecycle_rule'):

   try:
        s3 = boto3.resource('s3')
        bucket_lifecycle_configuration = s3.BucketLifecycleConfiguration(self._bucketname)
        response = bucket_lifecycle_configuration.put(
            LifecycleConfiguration={
                'Rules': [
                    {
                        'Expiration': {
                            'Days': 1
                        },
                        'ID': id_name,
                        'Status': 'Enabled',
                        'Filter': {'Prefix': ''},
                        'NoncurrentVersionExpiration': {
                            'NoncurrentDays': 1
                        },
                        'AbortIncompleteMultipartUpload': {
                            'DaysAfterInitiation': 1
                        }
                    }
                ]
            }
        )
    except Exception as e:
        logging.error(e)
        raise e
    return response

When I add "ExpiredObjectDeleteMarker': True" in expiration block, I got an XML error (malformed). Official documentation says to put this in this block. Any idea ? Thanks guys

posta 2 anni fa2228 visualizzazioni
2 Risposte
1
Risposta accettata

Hi there!
You cannot add the ExpiredObjectDeleteMarker configuration within the same expiration rule where you specify a Days or Date value. Try having two separate rules for the LifeCycleConfiguration. Something like this:

response = bucket_lifecycle_configuration.put(
            LifecycleConfiguration={
                'Rules': [
                    {
                        'Expiration': {
                            'Days': 1
                        },
                        'ID': 'Rule01',
                        'Status': 'Enabled',
                        'Filter': {'Prefix': ''},
                        'NoncurrentVersionExpiration': {
                            'NoncurrentDays': 1
                        },
                        'AbortIncompleteMultipartUpload': {
                            'DaysAfterInitiation': 1
                        }
                    },
                    {
                        'Expiration': {
                            'ExpiredObjectDeleteMarker': True
                        },
                        'ID': 'Rule02',
                        'Status': 'Enabled',
                        'Filter': {'Prefix': ''}
                    }
                ]
            }
        )
AWS
Fabio_L
con risposta 2 anni fa
  • Hi ))) Yes I should have thought about that. This is the solution Thanks Fabio

0

The documentation for S3.BucketLifecycleConfiguration.put() states:

ExpiredObjectDeleteMarker (boolean) --

Indicates whether Amazon S3 will remove a delete marker with no noncurrent versions. If set to true, the delete marker will be expired; if set to false the policy takes no action. This cannot be specified with Days or Date in a Lifecycle Expiration Policy.

-- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.BucketLifecycleConfiguration.put (emphasis mine)

Specifying 'Expiration': { 'Days': 1 } causes current versions to expire after one day.

The 'NoncurrentVersionExpiration': { 'NoncurrentDays': 1 } block specifies that non-current versions are deleted one day after Expiration.

AWS
con risposta 2 anni fa
  • Hi thanks for the reply

    Yes sure this is the code I wrote. The question is about the expired object delete marker)))

    I have read the doc before writing the code.

    With the AWS console I can set up the lifecycle rule for expired object delete markers but not on code...

    When some buckets can't be delete with the console, the only solution is to put a lifecycle rule for deleting

    • current versions
    • non current versions
    • expired object delete markers

    according to AWS documentation...

    So the issue remains

    Thanks for the help

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande