Search code examples
amazon-web-servicesamazon-s3django-storage

How to download media file from S3 bucket in Django


I'm trying to download files from my media directory in S3. I've the following download code:

def download_document(request, pk):
    service = Service.objects.get(pk=pk)
    file_path = os.path.join(settings.MEDIA_ROOT, service.pdf_file.name)
    print(file_path)
    file_mimetype = mimetypes.guess_type(file_path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type=file_mimetype)
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response

    raise Http404

Within settings I set MEDIA_ROOT as follows:

if 'RDS_HOSTNAME' in os.environ:
    AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
    AWS_S3_OBJECT_PARAMETERS = {
        'CacheControl': 'max-age=86400',
    }
    DEFAULT_FILE_STORAGE = 'sme.storage_backends.MediaStorage'
    MEDIA_ROOT = 'https://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
else:
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

So when I run locally I am storing the media locally but when I run on AWS I'm storing in an S3 bucket. It works as I expect locally but when I try and apply the s3 bucket I get the file not existing


Solution

  • first thing I would check is whether you can actually anonymously retrieve the media file from S3 to discard any permission-related issue. Use curl or wget.

    If you can't, then you have two options:

    • Change bucket policy and ACL to be public.
    • Set AWS credentials and use boto3 to retrieve the content from S3.