gpt4 book ai didi

python - 带有 s3 后端的默认 django-ajax-uploader 给出 MalformedXML 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:53 25 4
gpt4 key购买 nike

我设置了一个测试脚本,几乎与这里的示例完全一样: https://github.com/GoodCloud/django-ajax-uploader

似乎开始上传文件(javascript 更新文件的名称和大小),但是 View 给我一个 500 错误消息。我找不到任何关于如何修复它的信息。

S3ResponseError: S3ResponseError: 400 Bad Request
<Error><Code>MalformedXML</Code><Message>The XML you provided was not well-formed or did not validate against our published schema</Message><RequestId>26E6EF8296A0E585</RequestId><HostId>F4QUOsVT4LxC+6OUP2lE1/9uPC77keOejyWs57GpS5kjvHXpun3U+81ntL8ZTgDa</HostId></Error>

我能够使用此处的命令使用 boto 在 shell 中上传文件: Upload 0 byte file to Amazon S3

View :

from ajaxuploader.views import AjaxFileUploader
from ajaxuploader.backends.s3 import S3UploadBackend

import_uploader = AjaxFileUploader(backend=S3UploadBackend)

JavaScript:

var uploader = new qq.FileUploader({
action: "/ajax/profile-upload/",
element: $('#file-uploader')[0],
multiple: true,
onComplete: function(id, fileName, responseJSON) {
if(responseJSON.success) {
alert("success!");
} else {
alert("upload failed!");
}
},
onAllComplete: function(uploads) {
// uploads is an array of maps
// the maps look like this: {file: FileObject, response: JSONServerResponse}
alert("All complete!");
},
params: {
'csrf_token': $('[name=csrfmiddlewaretoken]').val(),
'csrf_name': 'csrfmiddlewaretoken',
'csrf_xname': 'X-CSRFToken',
},
});

模板:

<div id="file-uploader">       
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
</noscript>
</div>

我的 settings.py 文件中有 s3 访问变量(它们在 ajaxuploader/backends/s3.py 文件中调用):

AWS_ACCESS_KEY_ID = myAccessKey
AWS_SECRET_ACCESS_KEY = mySecretKey
AWS_BUCKET_NAME = bucketName

最佳答案

我使用覆盖上传功能的自定义 s3 后端解决了这个问题,并使用 django-storages 而不是 boto 来保存文件。试试这个:

from ajaxuploader.backends.base import AbstractUploadBackend
from django.core.files.storage import default_storage

class S3CustomUpload(AbstractUploadBackend):
NUM_PARALLEL_PROCESSES = 4

def upload_chunk(self, chunk):
#save file to s3
self._fd.write(chunk)
self._fd.close()

def setup(self, filename):
self._fd = default_storage.open('%s/%s' % ('uploads/materials/', str(filename)), 'wb')

def upload(self, uploaded, filename, raw_data, *args, **kwargs):
try:
if raw_data:
# File was uploaded via ajax, and is streaming in.
chunk = uploaded.read(self.BUFFER_SIZE)
while len(chunk) > 0:
self.upload_chunk(chunk, *args, **kwargs)
chunk = uploaded.read(self.BUFFER_SIZE)
else:
# File was uploaded via a POST, and is here.
for chunk in uploaded.chunks():
self.upload_chunk(chunk, *args, **kwargs)
return True
except:
# things went badly.
return False

def upload_complete(self, request, filename, *args, **kwargs):
upload = Upload()
upload.upload = settings.S3_URL + "uploads/materials/"+ filename
upload.name = filename
upload.save()

return {'pk': upload.pk}

关于python - 带有 s3 后端的默认 django-ajax-uploader 给出 MalformedXML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17160635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com