My web app is image-centric and when a user uploads an image (any size), I need to create a thumbnail and store so that I can use the thumbnail and not the original image. I use AWS S3 bucket, boto3, django-storages. The file upload works flawlessly, my issue is when I generate a thumbnail and upload to the S3 bucket with different file name (it does not throw error, but I cannot see any thumbnail images generated or stored)
This is my settings.py
Media root
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
AWS Specific settings (I did not override MEDIA_ROOT)
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME')
AWS_QUERYSTRING_AUTH = False
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_DEFAULT_ACL = os.getenv('AWS_DEFAULT_ACL')
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_S3_FILE_OVERWRITE=False
# s3 static settings
STATIC_LOCATION = 'static'
STATIC_ROOT = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
CKEDITOR_BASEPATH = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/ckeditor/ckeditor/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# s3 public media settings
PUBLIC_MEDIA_LOCATION = 'media'
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/'
#MEDIA_ROOT = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
The code where I upload generated thumbnail files (if it ever generates):
I use the Pillow library for generating thumbnail
Version 1 of code:
tfname = os.path.join(os.path.join(settings.BASE_DIR,"media"), file_name)
image.save(tfname)
Version 2 of the code (saw it somewhere on this site and tried):
import boto3
s3 = boto3.resource('s3')
s3.Bucket(settings.AWS_STORAGE_BUCKET_NAME).upload_file('/{0}'.format(file_name),file_name)
Neither of them works, not throw any exception/error