neekey's blog


Direct to S3 File Uploads in Python

The general tutorial from Heroku:

The main benefit for this approach is that AWS will now handle the process of the actually uploading, so that our server does not need to worry about the performance and it’s scalable.

Basic work flow:

  • Setup AWS credential for your instance so that it has the access to your S3 bucket
  • Create your S3 bucket to be used to upload. (CROS need to be set for the client to do the upload)
  • Create an API in your server for the client to get the generated presigned S3 upload URL:
  • The client uses the URL to upload files and get the file S3 readable URL
  • The client make another call to the server to save the url to the database

More details

S3 Bucket

You don’t need to make the S3 bucket public, you can just keep the policy empty. For the CROS, set it to allow traffic from your own domain, can you might also allow the header of “Location”, the value of which will be the newly created URL

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="">

Double check your credential

You will be able to create the presaged S3 URL even you don’t have a correct credentials, but the URL will not be valid. That what got me stuck when I kept getting “Access Denied” when trying to upload. If that happens to you, double check your credentials. (

Adding Restrictions

By pass the uploading process all to AWS, we lost the control of what actual files the users are able to upload. But still, AWS provides setting to restrict content length and file type or the acl (

    Fields={"acl": "public-read", "Content-Type": file_type}, 
        {"acl": "public-read"}, 
        {"Content-Type": file_type}, 
        ["content-length-range", 0, maximum_file_size] 

How to handle Other Security Issues?

As the project I’m working on is just a MVP, so I didn’t add to much work on this part, but even with the configs you can add with the generate_presigned_post, the user can always fake their file types.

My idea of prevent potential malicious files is using Lambda to listen to the event of the S3 bucket. Once a new file is uploaded, the lambda can do extra file content check and scan. At the same time, the newly created url once saved to the database, it can be marked as “being verified” so that it won’t be used to the application immediately. Once the lambda finishes all the check, it can call the server to make the upload as safe.

Tools & Services I Use

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.