I am trying to create a super simple file upload script using the boto library, not any others. From what I have tried it feels like it should work, but it doesn't.
The error I am getting now is:
S3ResponseError: 400 Bad Request
Here is the code I have in my view:
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
file = request.FILES['file']
filename = file.name
conn = boto.connect_s3()
bucket = conn.create_bucket('some-bucket-name')
from boto.s3.key import Key
k = Key(bucket)
k.key = filename
k.send_file(file)
k.content_type = mimetypes.guess_type(filename)[0]
k.set_contents_from_stream(file.chunks())
k.set_acl('public-read')
return HttpResponseRedirect('/')
else:
form = UploadFileForm()
return render_to_response('home/upload.html',
{'form':form},
context_instance=RequestContext(request))
If I modify it to save locally it works so it is the upload to s3 that is broken. I have tested set_contents_from_string
and that works for string data. However, anything which deals with files or streams I get the above error. Am I missing a setting somewhere or is what I am doing just completely wrong?