简体   繁体   English

使用Django从Amazon S3下载文件

[英]Downloading files from amazon s3 using django

I am new to the Django framework , I was trying to generate a link to download files from Amazon S3 . 我是Django框架的新手,我试图生成一个链接以从Amazon S3下载文件。 I got this error when I tried to load the template page: 尝试加载模板页面时出现此错误:

Reverse for 'myapp.views.handles3downloads' with arguments '(u'README.md',)'
and keyword arguments '{}' not found.

urls.py urls.py

urlpatterns = patterns('',
    url(r'^handles3downloads/(\d+)/$', handles3downloads),
)

views.py views.py

def handles3downloads(request, fname):
    bucket_name = 'bucketname'
    key = s.get_bucket(bucket_name).get_key(fname)
    dfilename = key.get_contents_to_filename(fname)

    wrapper = HttpResponse(file(dfilename))
    response = HttpResponse(wrapper, content_type='text/plain')
    response['Content-Length'] = os.path.getsize(dfilename)
    return response

template file 模板文件

<a href="{% url 'myapp.views.handles3downloads' sfile.linkUrl %}">{{sfile.linkUrl}}</a>

I looked at some of the solutions with similar errors but it didn't help me. 我查看了一些具有类似错误的解决方案,但对我没有帮助。 Can anyone help me out please. 谁能帮帮我。

Advance thanks 预先感谢

Your regular expression in the urls.py file seems to be wrong. 您在urls.py文件中的正则表达式似乎是错误的。 Try using this instead: 尝试使用此代替:

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

You're passing parameter string to the view, and the regex is matching integers. 您正在将参数字符串传递给视图,并且regex匹配整数。

Jordan is correct, there is a problem with your urls.py. 乔丹是正确的,您的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? 您正在尝试在“ myapp.views.handles3downloads”上获取一个反向字符串,但是是否已识别出该反向字符串? Try this. 尝试这个。

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

urls.py urls.py

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

views.py 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM