Check the expiration date of objects with AWS CLI

We can move objects to other storage classes, archive objects, or delete objects using the Lifecycle Policy.

In this post, we will introduce how to check the expiration date of an object using head-object when the Lifecycle Rule is set.

Check the lifecycle configuration information set on the bucket

note: Please refer to How Do I Create a Lifecycle Policy for an S3 Bucket? for lifecycle setting.

You can check the lifecycle configuration information set on the bucket with get-bucket-lifecycle-configuration.

$ aws s3api get-bucket-lifecycle-configuration --bucket ${bucket_name}
{
    "Rules": [
        {
            "Expiration": {
                "Days": 60
            },
            "ID": "lifecycle-test-rule",
            "Filter": {
                "Prefix": ""
            },
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 30
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 7
            }
        }
    ]
}

In this case, the objects will be deleted after 60 days.

Check the expiration date of the object

You can check the expiration date of the object with head-object.

$ aws s3api head-object --bucket ${bucket_name} --key ${key}
{
    "AcceptRanges": "bytes",
    "Expiration": "expiry-date=\"Sun, 14 Oct 2018 00:00:00 GMT\", rule-id=\"lifecycle-test-rule\"",
    "LastModified": "Tue, 14 Aug 2018 19:57:04 GMT",
    "ContentLength": 641,
    "ETag": "\"f4cb543f3d9a2fcf9203dc39598c7d88\"",
    "VersionId": "null",
    "ContentEncoding": "utf-8",
    "ContentType": "text/plane",
    "ServerSideEncryption": "AES256",
    "Metadata": {}
}

We could confirm the expiration date of the object from the Expirationfield.

note: LastModified is Tue, 14 Aug 2018 19:57:04 GMT, but the Expiration is set to Sun, 14 Oct 2018 00:00:00 GMT Please refer to the document below.

Lifecycle Configuration Elements | Lifecycle Rules: Based on an Object's Age

Amazon S3 calculates the expiration time by adding the number of days specified in the rule to the time when the new version of the object is created, rounding the resulting time to the next day’s midnight UTC time. For example, in a bucket, if you have the current version of an object created at 1/1/2014 10:30 AM UTC, and the new version of the object that replaces the current version is created at 1/15/2014 10:30 AM UTC and you specify 3 days in a transition rule, then the transition date of the object would be calculated as 1/19/2014 00:00 UTC.

Conclusion

We introduced how to confirm the expiration date of an object. It is easy to check the expiration date because it can be acquired with the AWS API so please try to use it.

References