I'm saving images to my s3 bucket, generate a presigned URL and save that as a field in my model schema. When my frontend retrieves the model objects from the backend it uses that URL to retrieve the images.
The max expiration date you can set with v4 is 7 days. So what is the best practice to handle the "refreshing" of those URLs?
Things I thought about:
- Having a cron job in my backend that checks every e.g. every 24 hours if I have any URLs going invalid within the next 24h and generate a new one in case.
- Checking it everytime I receive a GET request and re-generate if it's invalid
- Not checking at all on the backend, and just try to the hit the URL on the frontend and if I get a 403 to then request the re-generation from the backend. I don't like this idea that much though as I couldn't simply use "/> but have to wrap it in some other logic.
I'm saving images to my s3 bucket, generate a presigned URL and save that as a field in my model schema. When my frontend retrieves the model objects from the backend it uses that URL to retrieve the images.
The max expiration date you can set with v4 is 7 days. So what is the best practice to handle the "refreshing" of those URLs?
Things I thought about:
- Having a cron job in my backend that checks every e.g. every 24 hours if I have any URLs going invalid within the next 24h and generate a new one in case.
- Checking it everytime I receive a GET request and re-generate if it's invalid
- Not checking at all on the backend, and just try to the hit the URL on the frontend and if I get a 403 to then request the re-generation from the backend. I don't like this idea that much though as I couldn't simply use "/> but have to wrap it in some other logic.
1 Answer
Reset to default 11The best practice is do not store pre-signed URLs.
Instead, generate them on-the-fly when they are required. They only take a couple of lines of code to generate, and generation does not involve a call to AWS.
So, whenever the app needs to reference a pre-signed URL, immediately generate and use it. There will be no need to worry about expiration times.