简体   繁体   中英

Boto + Python + AWS S3: How to get last_modified attribute of specific file?

It's possible to get last_modified attribute:

import boto3

s3 = boto3.resource('s3')

bucket_name = 'bucket-one'
bucket = s3.Bucket(bucket_name)

for obj in bucket.objects.all():
    print obj.last_modified

But it does so for all the objects in a bucket. How can I get the last_modified attribute for one specific object/file under a bucket?

bucket_name = 'bucket-one'
file_name = 'my_file'

object = s3.Object(bucket_name, file_name)
print object.last_modified

or simply:

print s3.Object(bucket_name, file_name).last_modified

Hope this helps you....

This will get the last modified time of a particular object in an S3 bucket

import boto3
import time
from pprint import pprint

s3 = boto3.resource('s3',region_name='S3_BUCKET_REGION_NAME')
bucket='BUCKET_NAME'
key='OBJECT_NAME'
summaryDetails=s3.ObjectSummary(bucket,key)
timeFormat=summaryDetails.last_modified
formatedTime=timeFormat.strftime("%Y-%m-%d %H:%M:%S")
pprint( 'Bucket name is '+ bucket + ' and key name is ' + key + ' and last modified at time '+ formatedTime)

Thanks....

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