hardcoded url issue when drag & drop images with django-markdownx through S3

134 views Asked by At

My website works perfectly fine with django-markdownx, unless I upload images.

When I drag and drop image on markdownx form, auto generated image url is added in form like below:

enter image description here

As you see, image is shown just fine. My storage is AWS s3, and I'm using private bucket.

The problem occurs, however, an hour later. In the markdown url query parameter X-Amz-Expires=3600, which is an hour. So after that the url is no longer valid, saying request has expired.

enter image description here

This is another expired url, but you get the idea.

I use django-storages, boto3, AWS S3 for file storages. According to django-storages doc,

AWS_QUERYSTRING_EXPIRE (optional; default is 3600 seconds)

The number of seconds that a generated URL is valid for.

I might extend expiry time like super long as suggested in other post in SO, but doesn't that mean I should update at least once every super long time period? That doesn't seem the right way.

Some suggested making S3 bucket public, but I don't want allow anyone to download my image.

I delved in django-markdownx doc and github, without making much progress.

How can I get dynamically made presigned url when uploading image using djang-markdownx? Let me know if I'm missing anything or any suggestion is welcome.

Underneath are my django files

# settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')

AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None

AWS_S3_REGION_NAME = "ap-northeast-2"
# AWS_S3_SIGNATURE_VERSION = "s3v4"
# AWS_QUERYSTRING_AUTH = False

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

0

There are 0 answers