Amazon S3 - How to Check if Presigned URL is Expired?

The URL has the time it expires at.

Signature Version 2

htt ps://bucket.s3.amazonaws.com/foo.txt?AWSAccessKeyId=AKIAABCDEFGHIJK&Expires=1508608760&Signature=xxxxxxxxxxx

Expires gives the time in Unix timestamp (in seconds) and Coordinated Universal Time (UTC) .

$ date -d @1508608760
Sat Oct 21 17:59:20 UTC 2017

You can extract the value and compare it with the current time in UTC [time()], then decide to regenerate or not.


Signature Version 4

htt ps://s3.amazonaws.com/bucket/foo.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256& X-Amz-Expires=3600&X-Amz-Credential=AKIAJRZXXXXXXXXus-east-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Date=20171021T190750Z&X-Amz-Signature=8b84ae9b59e9f8a8d7066ecc39e797c8dc29848abcdef61717

X-Amz-Date gives the UTC time in ISO 8601 format.

You can extract the value, convert it to epoch/UTC and compare it with the current time in UTC [time()], then decide to regenerate or not.


The accepted answer isn't quite right for Signature Version 4.

Read on if you landed here looking for a way to find the expiry in js.

import { parseISO, addSeconds } from 'date-fns';

// get the query params: https://stackoverflow.com/a/901144/9362404
const params = new Proxy(new URLSearchParams(signedUrl), {
    get: (searchParams, prop) => searchParams.get(prop),
});
const creationDate = parseISO(params['X-Amz-Date']);
const expiresInSecs = Number(params['X-Amz-Expires']);

const expiryDate = addSeconds(creationDate, expiresInSecs);
const isExpired = expiryDate < new Date();

It's pretty obvious what's going on once you list out the query params:

// https://bucketname.s3.ap-southeast-2.amazonaws.com/file-name.pdf
// X-Amz-Algorithm=     AWS4-HMAC-SHA256
// X-Amz-Credential=    <aws-access-key>/20220722/ap-southeast-2/s3/aws4_request
// X-Amz-Date=          20220722T101759Z
// X-Amz-Expires=       259200
// X-Amz-Signature=     gobbeldygook-signature
// X-Amz-SignedHeaders= host

On macOS, use date -r 1535416265.