我正在使用boto3和django-storages将文件上传到Amazon S3,但是我无法正常工作,它总是以ConnectionClosedError结尾,但是当我使用django管理界面上传文件时,它可以正常工作,并且我在Amazon S3存储桶中看到了上传的文件。我试图找到解决方案,但没有成功,可能是什么错误?
这是我的html
<form method="post" action="{% url 'upload-document' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-row">
<label for="description">Description</label>
<input id="description"
type="text"
name="description"
value="{{ form_details.description }}"
required>
</div>
<input name="document_type" type="hidden" value="{{ document_type|cut:" " }}">
<input name="document_type_id" type="hidden" value="{{ document_type_id }}">
<div style="margin-top: 20px;" class="form-row">
<label for="file">File</label>
<input id="file"
accept=".pdf"
type="file"
name="document_file"
required>
</div>
<button style="margin-top: 20px; width: 100%;" class="button primary" type="submit">UPLOAD</button>
</form>
查看执行上传的代码
description = request.POST['description']
document_type = request.POST['document_type']
document_type_id = request.POST['document_type_id']
document = Document.objects.create(description=description,
document_type=document_type)
document_file = request.FILES['document_file']
document.document_file = document_file
if document_type_id != 0:
document.document_type_id = document_type_id
document.save()
以下是我的Amazon S3配置文件
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = False
DEFAULT_FILE_STORAGE = 'app.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'app.aws.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucketname'
S3DIRECT_REGION = 'eu-west-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL