user1328021
user1328021

Reputation: 9850

Using boto for S3 upload I'm getting a EOF error

I'm trying to upload a file to S3 (actually the upload works) but my app crashes and throws the error saying:

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

I saw that some ways people have solved the problem was adding rewind=True to their set_contents_from_string call. However this throws the following error:

set_contents_from_string() got an unexpected keyword argument 'rewind'

The following is the content of my views.py. What am I doing wrong?

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)
            ...

Upvotes: 4

Views: 3027

Answers (2)

daroo
daroo

Reputation: 342

See the final comment here. Presuming that you're using Django-Storages, just upgrade to the latest version, should fix the issue.

Upvotes: 2

after

filecontent = file.read()

Put:

file.seek(0)

Upvotes: 2

Related Questions