[Answered ]-Downloading files from amazon s3 using django

0👍

urls.py

url(r'^handles3downloads/', handles3downloads),

views.py

def handles3downloads(request):
  fname = request.GET['filename']
  bucket_name = 'bucketname'
  key = s.get_bucket(bucket_name).get_key(fname)
  key.get_contents_to_filename('/tmp/'+key.name)
  wrapper = FileWrapper(open('/tmp/'+fname, 'rb'))
  content_type = mimetypes.guess_type('/tmp/'+fname)[0]
  response = HttpResponse(wrapper,content_type=content_type)
  response['Content-Length'] = os.path.getsize('/tmp/'+fname)
  response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(fname)

templates

<a href="/handles3downloads/?filename=file1.jpg" rel="external">Download</a>
👤Niya

2👍

Your regular expression in the urls.py file seems to be wrong. Try using this instead:

url(r'^handles3downloads/(\w+)/$', handles3downloads),

You’re passing parameter string to the view, and the regex is matching integers.

0👍

Jordan is correct, there is a problem with your urls.py. You can tell by the error. You are trying to get a reverse on ‘myapp.views.handles3downloads’, but has that reverse string been identified? Try this.

urlpatterns = patterns('',
     url(r'^handles3downloads/([^/]+)/$', handles3downloads,
     name='myapp.views.handles3downloads'),
)
👤Sepero

Leave a comment