Search code examples
pythondjangoamazon-s3digital-oceandjango-storage

changing 'DEFAULT_FILE_STORAGE' causing high TTFB ( waiting time ) with boto3


My settings

...
AWS_ACCESS_KEY_ID = 'MY_KEY'
AWS_SECRET_ACCESS_KEY = 'MY_SECRET_KEY'
AWS_STORAGE_BUCKET_NAME = 'wallpapers'
AWS_S3_ENDPOINT_URL = 'https://sgp1.digitaloceanspaces.com'
AWS_S3_CUSTOM_DOMAIN = 'wallpapers.sgp1.cdn.digitaloceanspaces.com'

AWS_QUERYSTRING_AUTH = False

AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
AWS_DEFAULT_ACL = 'public-read'

STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
MEDIA_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, 'media')
MEDIA_ROOT = 'media/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
...

when I open 'http://127.0.0.1:8000/'

this causes high TTFB...

enter image description here

but when I comment out/ remove this in settings.py... #DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

then everything works fine

low TTFB


Solution

  • I realized the real problem was caused by a template tag.

    <p>{{ my_img.width }}</p>
    

    so in order to get the width of the image stored in MySQL, it was downloading the chunks of the image and then calculating the dimensions. to overcome this I added separate fields in my model.py for image height and width. it worked!!! I m new to Django. it might not be the best approach. please let me know for a better solution.