使用boto进行S3上传我收到了EOF错误

时间:2012-11-01 18:43:21

标签: django amazon-s3 boto

我正在尝试将文件上传到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)
            ...

2 个答案:

答案 0 :(得分:2)

filecontent = file.read()

把:

file.seek(0)

答案 1 :(得分:2)

查看最终评论here。假设您正在使用Django-Storages,只需升级到最新版本,就可以解决问题。