Python django从列表下载文件(s3作为后端)
我需要列出一个包含S3 bucket内容的html表,并启用下载文件的选项。为此,我做了以下工作: 代码正确地显示了bucket文件的列表,但我不确定如何编写代码来下载这些文件 inventory.html:Python django从列表下载文件(s3作为后端),python,django,django-views,django-templates,boto3,Python,Django,Django Views,Django Templates,Boto3,我需要列出一个包含S3 bucket内容的html表,并启用下载文件的选项。为此,我做了以下工作: 代码正确地显示了bucket文件的列表,但我不确定如何编写代码来下载这些文件 inventory.html: {% block title %} title{% endblock %} {% block content %} <table class="table table-striped"> <thead> <tr> <th
{% block title %} title{% endblock %}
{% block content %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Date <a href="?order_by=ord_date&direction=desc">{% load static %}<img src="{% static "arrow.png" %}" width="12" height="12" alt="order desc"></a><a href="?order_by=ord_date&direction=asc">{% load static %}<img src="{% static "sort-down.png" %}" width="12" height="12" alt="order asc"></a></th>
<th scope="col">Name <a href="?order_by=ord_source&direction=desc">{% load static %}<img src="{% static "arrow.png" %}" width="12" height="12" alt="order desc"></a><a href="?order_by=ord_source&direction=asc">{% load static %}<img src="{% static "sort-down.png" %}" width="12" height="12" alt="order asc"></a></th>
<th scope="col">Size</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td>{{ item.LastModified }}</td>
<td>{{ item.Key }}</td>
<td>{{ item.Size }}</td>
<td><button type="Button" class="btn btn-secondary btn-sm"><a href="?key_download={{ item.Key }}">Download</button></a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
所以我不知道我怎么才能把它还回去。
请记住,模板或html上的列表包含对象列表,而不是静态内容
如果有其他方法,我非常感激。
非常感谢。您可以返回重定向,使用正确的ACL,它应该可以开始下载
返回HttpResponseRedirect(url)
client = boto3.client('s3')
def inventory(request):
if request.GET.get('key_download'):
url = client.generate_presigned_url('get_object', Params = {
'Bucket':'url_of_the_bucket',
'Key':request.GET.get('key_download')},
ExpiresIn = 86400)
return **"I don't know how I can return it"**
else:
fdc_inventories = client.list_objects_v2(Bucket='url_of_the_bucket')
fdc_inventories['Contents'].sort(key=itemgetter('LastModified'), reverse=True)
return render(request, 'inventory.html', {'items': fdc_inventories['Contents']})