Uploading a Photo to an S3 Bucket with the AWS SDK and React

DSL
2 min readJan 5, 2023

--

Here is a step-by-step guide for creating an AWS account, creating a key pair, setting up the AWS CLI on your local machine, and creating an S3 bucket with public permissions using the AWS CLI:

  1. Go to the AWS home page (https://aws.amazon.com/) and click on the “Create a Free Account” button.
  2. Follow the prompts to create an AWS account. This will involve providing your contact information, as well as agreeing to the AWS Customer Agreement and AWS Service Terms.
  3. Once your AWS account has been created, log in to the AWS Management Console.
  4. In the AWS Management Console, go to the EC2 dashboard and click on the “Key Pairs” link in the left sidebar.
  5. Click the “Create Key Pair” button, give your key pair a name, and then download the key pair file. Make sure to save the key pair file in a safe place, as it will be required to connect to your EC2 instances.
  6. To set up the AWS CLI on your local machine, follow the instructions for your operating system in the AWS CLI documentation: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
  7. Once the AWS CLI is installed, you will need to configure it with your AWS access keys. You can find your access keys in the AWS Management Console by going to the “My Security Credentials” page.
  8. To create an S3 bucket with public permissions using the AWS CLI, you can use the following command:
aws s3api create-bucket --bucket my-bucket --acl public-read

This will create an S3 bucket with the name “my-bucket” and set the permissions to “public-read”, which allows anyone to view the contents of the bucket.

To write a React application that uses the S3 API to upload a photo, you will need to do the following:

  1. Install the AWS SDK for JavaScript:
npm install aws-sdk
  1. Import the AWS SDK and the S3 client in your React component:
import AWS from 'aws-sdk';
const s3 = new AWS.S3();
  1. Create a file input field that allows the user to select a photo to upload:
<input type="file" onChange={e => setFile(e.target.files[0])} />
  1. Create a function that handles the file upload when the user clicks a submit button:
const handleSubmit = async e => {
e.preventDefault();
const fileName = file.name;
const params = {
Bucket: 'my-bucket',
Key: fileName,
Body: file,
ACL: 'public-read'
};
try {
const uploadedFile = await s3.upload(params).promise();
console.log(uploadedFile);
} catch (err) {
console.error(err);
}
};
return (
<form onSubmit={handleSubmit}>
<input type="file" onChange={e => setFile(e.target.files[0])} />
<button type="submit">Upload</button>
</form>
);

This code creates an S3 client and defines the parameters for the file upload, including the bucket name, key (file name), file body, and ACL (access control list). When the form is submitted, the file is uploaded to the specified S3 bucket with the specified file name and public permissions.

--

--

DSL
DSL

Written by DSL

Sr software engineer. Love in Go, JavaScript, Python, and serverless AWS. Follow me for tech insights and experiences. follow me on twitter @terraformia