about gallery disclaimer

S3 Image/Video Upload React Component with AWS Amplify


Running App Example

S3 Image/Video upload into Amazon S3 requires Amazon Cognito for user authentication. Once user Signed-Up and Signed-In, the user can start to see file upload form:

Screen Shot 2020 06 09 at 2 20 56 PM

On this blog, we will utilize AWS Amplify to setup Amazon S3 and Amazon Cognito backend, and integrate it with our React component front-end.


Create React App

Create React App using NPX/NPM CLI, follow this guide for start:

Add Amplify, S3, and Cognito

Add Amazon Cognito support to your App by using Amplify CLI from your App directory as follow:

prompt$ cd app-directory

prompt:~app-directory$ amplify init 
(follow the guide to initialize amplify backend configuration)

prompt:~app-directory$ amplify add storage 
(follow the guide, this will also add Cognito support during the guide)

prompt:~app-directory$ amplify push 
(pushing your Cognito configuration to AWS back-end using CloudFormation. Cognito User Pools and configuration will be created)



We will use simple HTML/JSX input form with type=file. Below are the code snippet for the form section. The full App.js file can be found here:

//---- code snippet ----//
      <div className='video-uploader'>
        <form onSubmit={(e) => onSubmit(e)}>
            <label className='select-label'>Select video: </label>
              accept='image/*, video/*'
              onChange={(e) => onChange(e)}
          <button type='submit' className='btn'>
            Submit <MdSend className='btn-icon' />
//---- code snippet ----//
Importing Amplify, Storage (S3), and Cognito Authentication

Once pre-requisites above done, we can start importing Amplify, Storage (S3), and Cognito objects into our code. Notice that for Cognito, we are importing the AmplifyAuthenticator, AmplifySignOut, AmplifySignIn, andAmplifySignUp UI components.

//---- code snippet ----//
import React, { useState } from 'react'
import './App.css'
import Amplify, { Storage } from 'aws-amplify'
import {
} from '@aws-amplify/ui-react'
import { MdSend /* MdList */ } from 'react-icons/md'
import awsConfig from './aws-exports'
//---- code snippet ----//
React Functional Component for Storage (S3) Upload

We will use 3 states to help us creating the functions, the states are: name (to record the file name), file (to record the file object that will be uploaded), and response (to record status of the upload process).

We will use 2 event handler: onChange() and onSubmit() from the HTML page.onChange() will change the state of file and name during user interaction of choosing the file, then once button submitted, onSubmit() will take both name and file state as argument to pass into Storage.put() method provided by Amplify, response state will also be updated along with the upload status result.

//---- code snippet ----//
const App = () => {
  const [name, setName] = useState('')
  const [file, setFile] = useState('')
  const [response, setResponse] = useState('')

  const onChange = (e) => {
    if ([0] !== null) {

  const onSubmit = (e) => {
    if (file) {
      Storage.put(name, file, {
        /* level: 'protected', */
        contentType: file.type,
        .then((result) => {
          setResponse(`Success uploading file: ${name}!`)
        .then(() => {
          document.getElementById('file-input').value = null
        .catch((err) => {
          setResponse(`Can't upload file: ${err}`)
    } else {
      setResponse(`Files needed!`)
  //---- code snippet ----//


As you can see, adding Storage (S3) Support, Authentication, Sign Up, Sign In, and Sign Out is very easy with AWS Amplify support. AWS handles the backend heavy-lifting to store your Image/Video, user information, SMS/E-Mail based verification, Passwords policy and much more, Happy Coding!


Discuss on