Simple & secure direct-to-S3 uploads from modern browsers

<p>The problem of providing users with a way to upload files to an S3 bucket is something you run into in every project. However, identifying the right JavaScript components and configuration and making it all work together to accomplish this task feels like black magic.</p> <p>The AWS documentation would have you set up an&nbsp;<a href="https://aws.amazon.com/cognito/" rel="noopener ugc nofollow" target="_blank">additional authentication service</a>&nbsp;and import the whole AWS JS SDK into your client code &mdash; but there&rsquo;s really no need for that!</p> <p>Instead, we can&nbsp;<strong>use pre-signed URLs</strong>&nbsp;and&nbsp;<strong>modern web APIs</strong>&nbsp;to easily upload direct to S3 from the browser with only a hundred lines of code or so.</p> <p>The overall solution, from the user&rsquo;s browser&rsquo;s perspective, looks like this:</p> <ol> <li>User clicks &ldquo;Upload file&rdquo; and selects a file</li> <li>Given this file&rsquo;s metadata, request&nbsp;<strong>a pre-signed S3 PutObject URL</strong>&nbsp;from your server-side API</li> <li>Given this pre-signed S3 URL, the browser PUTs the file using&nbsp;<code>XmlHttpRequest</code>&nbsp;and can monitor progress</li> <li>Once uploaded, the browser returns the new key back to the API again in order to trigger whatever your API does with uploaded files</li> <li>Profit!</li> </ol> <p>If you want to skip to the finished code, here&rsquo;s&nbsp;a gist with TypeScript frontend and Python API handler&nbsp;as examples.</p> <p>I&rsquo;ll go through step by step in this post:</p> <p><a href="https://medium.com/@taylorhughes/simple-secure-direct-to-s3-uploads-from-modern-browsers-f42695e596ba">Click Here</a></p>
Tags: API web