Test if an S3 object was created or updated when using boto3

Viewed 247

Is it possible to determine whether or not an S3 object was created or updated when using boto3?

When using boto3.client('s3').put_object() to create or update S3 objects, the underlying PutObject API action doesn't differentiate between creating and updating objects, so ResponseMetadata.HTTPStatusCode is always 200.

There is no post_object() method for boto3.client('s3'), nor is there a post method for boto3.resource('s3').object(). In fact, it doesn't appear that there is a PostObject API action anyway.

import boto3
import base
import base64

client = boto3.client('s3')
body = 'Hello World!'

response = client.put_object(
    Bucket='my-bucket',
    Key='my-key.json',
    Body=body.encode('utf-8'))

print('S3 object successfully created. Or was it updated????')
1 Answers

There is no such thing as "updating" an object. In S3 you always fully replace them, which means you have to upload entire "updated" object.

But if your bucket is versioned, you can always track the "update" status of your objects yourself, by checking their Date metadata attribute.

There is also Last-Modified attribute which you can inspect and compare with Date attribute to check if they are different.

Related