6

This is the code that handles the uploading request:

@app.route('/upload', methods=['POST'])
def upload():
    if request.method == 'POST':
        test = request
        data_file = request.files.get('file')
        file_name = data_file.filename
        conn = S3Connection(settings.ACCESS_KEY, settings.SECRET_KEY)
        bucket = conn.get_bucket(settings.BUCKET_NAME)
        k = Key(bucket)
        k.key = 'file_test.jpg'
        # k.set_contents_from_file(data_file)
        k.set_contents_from_string(data_file.readlines())

        # return jsonify(name=file_name)
        return jsonify(name=file_name)

I've tried 3 options:

k.set_contents_from_string(data_file.readlines())
k.set_contents_from_file(data_file)
k.set_contents_from_stream(data_file.readlines())

So what is the right way to upload files to amazon s3?

2
  • 1
    can you include the error you get? Btw try using set_contents_from_string(data_file.read()). Commented Jan 20, 2014 at 14:28
  • i put it in an answer, would you please accept it? Thanks. Commented Jan 21, 2014 at 14:10

2 Answers 2

6

Here is a fully-functioning example of how to upload multiple files to Amazon S3 using an HTML file input tag, Python, Flask and Boto.'

The main keys to making this work are Flask's request.files.getlist and Boto's set_contents_from_string.

Some tips:

  • Be sure to set S3 bucket permissions and IAM user permissions, or the upload will fail. The details are in the readme.
  • Don't forget to include enctype="multipart/form-data" in your HTML form tag.
  • Don't forget to include the attribute multiple in your HTML input tag.
  • Don't forget to store the AWS user's credentials in environment variables as shown in the readme. Make sure these environment variables are available in the session where Python is running.
Sign up to request clarification or add additional context in comments.

Comments

4

In your code in the following line:

k.set_contents_from_string(data_file.readlines())

you're sending a list of strings (terminated by newlines!) to Amazon instead of the file content as is.

You need to pass a single str object with the file contents:

set_contents_from_string(data_file.read())

2 Comments

I'm encountering this exact same issue however following your suggestion gives me an error: 'FileField' object has no attribute 'read'
@haye321 what is FileField? what are you using? What framework? Django? Flask+WTForms? Are you POSTing the form with enctype="multipart/form-data"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.