Bigair
Bigair

Reputation: 1592

Image URL django media file using AWS S3 expires after a while

I use S3 for media file storage on my Django project.

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

From my iOS project I receive image URLs from Django like

https://my-bucket.s3.amazonaws.com/image-name.jpg?AWSAccessKeyId=<AccesKey>&Signature=<Signiture>

It seems the image URLs are expired after a while(and they probably should). However this causes a problem. When iOS app user left the app and came back after a while and the app memory was alive, the app tries to load images from expired urls(ex. cell reuse) resulting request error.

I wonder what is a right solution for this.

Upvotes: 4

Views: 1976

Answers (1)

Marcin
Marcin

Reputation: 238597

Your link is so-called S3 presigned URL and their expiration can't be disabled. But you can make it longer. The maximum expiration time is 7 days provided the URL is created using IAM user. If you use IAM role or instance role to make the URLs the max times are 36 and 6 hours respectively.

If these times are not satisfactory, you have three general options:

  • modify your app so that it auto-refreshes expired links.
  • don't use pre-sgined URLs. Instead make the s3 object public which never expire.
  • again don't use pre-sgined URL, but instead of making your objects public, create CloudFront distribution to server your objects from the CF. This way your bucket and objects are private (not accessible directly), but through CF which can also improve load times of the files.

Upvotes: 4

Related Questions