Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check when is the last time S3 bucket has been updated?

Does S3 bucket has information in regard to when is the last time it has been updated? How can I find the last time any of the objects in the bucket were updated?

like image 865
Cory Avatar asked Mar 18 '16 20:03

Cory


People also ask

How do I know if my S3 has expired?

Amazon S3 Object Expiration Overview The prefix (e.g. "logs/") identifies the object(s) subject to the expiration rule, and the expiration period specifies the number of days from creation date (i.e. age) after which object(s) should be removed.

How long does it take for S3 bucket to update?

Resolution. Most objects replicate within 15 minutes. However, sometimes replication can take a couple of hours. In rare cases, the replication can take longer.

How do I find my S3 bucket details?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to view the properties for. Choose Properties. On the Properties page, you can configure the following properties for the bucket.


2 Answers

As others have commented, there's no magic bit of metadata that stores this information. You just have to loop over the objects.

Code to do that with boto3:

import boto3
from datetime import datetime

def bucket_last_modified(bucket_name: str) -> datetime:
    """
    Given an S3 bucket, returns the last time that any of its objects was
    modified, as a timezone-aware datetime.
    """
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(bucket_name)
    objects = list(bucket.objects.all())
    return max(obj.last_modified for obj in objects)
like image 190
Mark Amery Avatar answered Oct 26 '22 02:10

Mark Amery


There is no native support for bucket last modified time. The way I do it is to use aws cli , sort the output, take the bottom line and print the first 2 fields.

$ aws s3 ls mybucket --recursive | sort | tail -n 1 | cut -d ' ' -f1,2
2016-03-18 22:46:48
like image 43
helloV Avatar answered Oct 26 '22 01:10

helloV