I'm accessing bucket objects to get a subset of files and their last modified date to see if there's been any updates since files were last ingested into our system. I noticed a discrepancy in how we are accessing the modified date and how I think it should be done, but I don't know which date is correct or what each date actually represent.
I've created a dummy example below to illustrate the two methods I'm getting dates and where they are different. The first one uses list_objects_v2 and accesses the value in data['Contents']['LastModified']
. The second method uses head_object and accesses the value in data['Metadata']['last-modified']
. For the sake of example, assume they are both the same object foo/baz
.
import boto3
# Method 1 - using list_objects_v2
client = boto3.client('s3', region_name='us-west-2')
paginator = client.get_paginator('list_objects_v2')
operation_parameters = {'Bucket': 'my-bucket',
'Prefix': 'foo/baz'}
page_iterator = paginator.paginate(**operation_parameters)
filtered_iterator = page_iterator.search("Contents[?Size > `100`][]")
for key_data in filtered_iterator:
print(key_data['LastModified'])
- This will return a date like
2020-03-01
import boto3
# Method 2 - using head_object
client = boto3.client('s3', region_name='us-west-2')
obj_head = client.head_object(Bucket='my-bucket', Key='foo/baz')
print(obj_head['Metadata']['last-modified'])
- This will return a different date like
2020-02-01
import boto3
# Bonus method 3 - using head_object BUT not using 'Metadata'
client = boto3.client('s3', region_name='us-west-2')
obj_head = client.head_object(Bucket='my-bucket', Key='foo/baz')
print(obj_head['LastModified'])
- This will return the same date as Method 1 using
list_objects_v2
What is the cause of the discrepancy? What does each date represent? What is the correct way to access the timestamp an object was last modified?
question from:
https://stackoverflow.com/questions/65910164/what-is-the-difference-between-the-bucket-object-lastmodified-date-and-the-bucke 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…