[Answered ]-Django Boto and Upload to S3 is a 400 Bad Request

2👍

I ran into this exact problem trying to transfer a file to S3. Eventually, I figured out that I had to set the size property on the Key object before calling send_file.

k = Key(bucket)
k.key = 'some-key'
k.size = 12345
k.send_file(file)

The size can be found using seek and tell on the file. The following will find the size while preserving the current file position. In your case, you can dispense with remembering the current position and just seek back to zero after getting the file size.

position = file.tell()
file.seek(0, os.SEEK_END)
size = file.tell()
file.seek(position)
👤Sean

0👍

I’d be inclined to test your s3 connection and bucket creation step in the shell.

python manage.py shell

I’m curious as to whether or not it’s those steps that are tripping you up. For instance if the bucket name you specify isn’t globally unique you will receive an error (unsure if it would result in the error code you’ve received but that is the first place I’d check).

If that is the issue you might consider setting a bucket in the AWS Management Console, then connect to it from your view, and upload files using appropriate ‘folder-like’ keys based on the needs of your project (see: Amazon S3 boto – how to create a folder?).

Leave a comment