使用s3后端的默认django-ajax-uploader会导致MalformedXML错误 [英] Default django-ajax-uploader with s3 backend gives MalformedXML error

查看:203
本文介绍了使用s3后端的默认django-ajax-uploader会导致MalformedXML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I set up a test script almost exactly like in the example here: https://github.com/GoodCloud/django-ajax-uploader

似乎开始上传文件(javascript更新文件的名称和大小),但该视图给我一个500错误与此消息。我找不到任何解决方法。

It seems to start uploading the file (javascript updates the name and size of the file), but the view gives me a 500 error with this message. I can't find anything on how to fix it.

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中上传文件:
将0字节文件上传到Amazon S3

I was able to upload a file in the shell with boto using the commands here: Upload 0 byte file to Amazon S3

从ajaxuploader.views导入AjaxFileUploader
的视图:

The view:

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

import_uploader = AjaxFileUploader(backend=S3UploadBackend)

javascript:

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 /后端调用/s3.py文件):

I have the s3 access variables in my settings.py file (they are called in the ajaxuploader/backends/s3.py file):

AWS_ACCESS_KEY_ID = myAccessKey
AWS_SECRET_ACCESS_KEY = mySecretKey
AWS_BUCKET_NAME = bucketName


推荐答案

我用自定义的s3后端解决了这个问题,功能与使用django-storage而不是boto来保存文件。尝试这样:

I solved this problem with a custom s3 backend that override the upload function & use django-storages instead of boto to save files. try this :

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}

这篇关于使用s3后端的默认django-ajax-uploader会导致MalformedXML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆