CustardBun
CustardBun

Reputation: 3857

How to get age of S3 object I'm giving presigned URL for?

Our website is using pre-signed URLs for getting objects from S3.

    presigned_url = s3_client.generate_presigned_url(
        "get_object",
        Params={"Bucket": someBucket, "Key": somePath},
        ExpiresIn=600,
    )

This has been working well for us, and we now want to record metrics on the age of the S3 object that they'd be grabbing with this presigned URL / the last modified date.

The only thing I can think of doing something like grabbing the object first, and then getting the age, but then it seems inefficient to be grabbing the object just to grab the age (especially since now the latency is low since its just generating a presigned URL):

    response = s3_client.head_object(
        Bucket=someBucket, Key=somePath
    )
    last_modified_time = response["LastModified"]
    recordMetric(..., last_modified_time)

    presigned_url = s3_client.generate_presigned_url(
        "get_object",
        Params={"Bucket": someBucket, "Key": somePath},
        ExpiresIn=600,
    )

Is there a better way to do this or approach the issue?

Upvotes: 1

Views: 453

Answers (1)

Dennis Traub
Dennis Traub

Reputation: 51634

There’s no need to get the object, and in your code example you’re already doing it correctly. The head_object() function in your example retrieves metadata from the object without retrieving the object itself. To my knowledge, this is the most efficient way to retrieve the object metadata.

Upvotes: 2

Related Questions