Here we don’t have to subclass, because we can pass in an instance rather than refer to a class. But if you have some media that requires authentication before you can access it – say PDF resumes which are only accessible to members – then you don’t want S3BotoStorage’s default S3 ACL of public-read. Most of your media uploads – user avatars, for instance – you want to be public. Timeout = settings.AWS_QUERYSTRING_EXPIRE # Cache the result for 3/4 of the temp_url's lifetime. Result = super(CachedS3Boto3Storage, self).url(name) # No cached value exists, follow the usual logic Key = hashlib.md5(f"CachedS3Boto3Storage_".encode()).hexdigest() # Add a prefix to avoid conflicts with any other apps Settings.py # We subclass the default storage engine to add some cachingĭEFAULT_FILE_STORAGE = '3Boto3Storage'įrom 3boto3 import S3Boto3StorageĬlass CachedS3Boto3Storage(S3Boto3Storage): This way - we can return the same url for subsequent page requests, allowing the client browser to fetch from their cache. So we can cache this temporary url (as long as we don't cache it longer than it's valid). Temporary urls will be valid for AWS_QUERYSTRING_EXPIRE seconds (3600 by default). If you don't want to generate a temporary url:Īdd AWS_QUERYSTRING_AUTH = False to your settings.py If you still want a temporary url: Following are steps to be followed: Go to settings.py file of django project and add following. When AWS_QUERYSTRING_AUTH = True (which is the default), django will generate a temporary url each time we fetch the url. Once the cache is set up, the simplest way to use caching is to cache your entire site.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |