简体   繁体   中英

Best practice to handle aws s3 presigned URL expiration date

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 eg 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.

The 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM