Forums

flask app uploading documents to s3 using boto

I have a flask app with a url /documents for user to upload the documents. I am using boto to upload documents to my s3 account. I want to allow only .doc, .png, .jpeg, .jpg, .gif file types and file size greater than 10kb and not exceeding 2mb. I wanted both client side check and server side check.

For client side, I am using formvalidation.io library. I cannot check file size with this library but can check file types which works ok... On server side, I am trying to check the file extension and file size again as client side check can be bypassed easily.... . I can check the file extension from server side using :

app.config['ALLOWED_EXTENSIONS'] = ['pdf','jpg','jpeg','png','gif','doc','PDF','JPG','JPEG','PNG','GIF','DOC']
app.config['MAX_FILE_SIZE'] = 2000000
def check_file_extension(user_filename):
    if '.' in user_filename.filename and user_filename.filename.split('.')[-1] in app.config['ALLOWED_EXTENSIONS']:
        return True
    return False

However, in order to check the file size, I would need to upload to temp s3 or temp location on server, get the file size and then copy it again to the appropriate folder on s3. I tried filestack but its a bit costly for s3 uploads.

Is there any other way to do this as this is a common scenario? Any suggestions please?

Thanks

As you're using Flask, this Stack Overflow answer looks like it should explain what you need to do to limit file upload size on the server side.