Skip to content Skip to sidebar Skip to footer

How To Extract Last Modified Date Of Bucket S3

I need to extract the last modification of the bucket object: Currently my code returns me in list with this format: How can I get it to return in a similar format:

Solution 1:

This should work.

  1. store comma seperated values in a single variable say time_string=2021,3,23,19,43,18.

  2. pub_date=datetime.datetime.strptime(time_string,'%Y,%m,%d,%H,%M,%S').strftime('%Y-%m-%d %H:%M:%S').

  3. strftime('%Y-%m-%d %H:%M:%S') you can formate it any format as you like.

  4. Referce for python doc for these values https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

example code

string="2013-1-25"
datetime.datetime.strptime(string, '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13"

Solution 2:

It appears that you have some code calling list_objects().

If so, the LastModified field is already stored as a datetime. You can manipulate it however you want.

import boto3
from datetime import datetime

s3_client = boto3.client('s3')

response = s3_client.list_objects_v2(Bucket='BUCKETNAME')

forobjectin response['Contents']:

    # 2021-02-08 04:00:28+00:00print(object['LastModified'])

    # 2021-02-08 04:00:28print(object['LastModified'].strftime('%Y-%m-%d %H:%M:%S'))

Post a Comment for "How To Extract Last Modified Date Of Bucket S3"