4

我正在尝试将文件上传到 S3(实际上上传工作),但我的应用程序崩溃并抛出错误说:

fp is at EOF. Use rewind option or seek() to data start.

我看到人们解决问题的一些方法是添加rewind=True到他们的 set_contents_from_string 调用中。但是,这会引发以下错误:

set_contents_from_string() got an unexpected keyword argument 'rewind'

以下是我的内容views.py。我究竟做错了什么?

def store_in_s3(filename, filecontent):
    conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
    b = conn.create_bucket('mybucket')
    mime = mimetypes.guess_type(filename)[0]
    k = Key(b)
    k.key = filename
    k.set_metadata("Content-Type", mime)
    k.set_contents_from_string(filecontent)
    #k.set_contents_from_string(filecontent, rewind=True)
    k.set_acl("public-read")

def add_m(request, points=None):
    mname = request.GET.get ('mname')
    format = request.GET.get ('format')
    type = request.GET.get ('type')
    if request.method == "POST":
        formtoaddm = spiceform(request.POST, request.FILES)
        if formtoaddm.is_valid():
            new_m = formtoaddm.save(commit=False)
            new_m.adder = request.user
            mname = new_m.mname
            file = request.FILES['content']
            filename = file.name
            filecontent = file.read()
            store_in_s3(filename, filecontent)
            ...
4

2 回答 2

2

请参阅此处的最终评论。假设您使用的是 Django-Storages,只需升级到最新版本,应该可以解决问题。

于 2014-05-06T15:16:48.040 回答
2

filecontent = file.read()

放:

file.seek(0)
于 2013-10-28T15:14:12.763 回答