title: How to download media file from S3 bucket in Django
body: <p>I'm trying to download files from my media directory in S3.  I've the following download code:</p>
  
  <pre><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
  </code></pre>
  
  <p>Within settings I set MEDIA_ROOT as follows:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>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</p>