How to setup Amazon S3 upload provider in your Strapi app
Tags: strapi, aws, awss3, emailprovider
Published: 2021-03-11 00:17:36

Strapi, by default, stores all files in public/uploads. In a production environment, you definitely would utilize a service, such as AWS Simple Storage Service (S3). Besides that it makes your Strapi app a stateless application according to [the Twelve-Factor App methodology](https://12factor.net/processes).

In this article, we will learn how to setup AWS S3 upload provider in your Strapi app.

<b>Prerequisites :</b>
- A Strapi app (Learn [how to install Strapi](https://strapi.io/documentation/developer-docs/latest/getting-started/quick-start.html#_1-install-strapi-and-create-a-new-project)) 
- A package manager, such as [Yarn](https://classic.yarnpkg.com/en/docs/install/#debian-stable) or [NPM](https://nodejs.org/en/)

<b>Step 1 : Install Strapi AWS S3 upload provider</b>
Open your terminal, then run :
`yarn add strapi-provider-upload-aws-s3`
or
`npm i strapi-provider-upload-aws-s3 --save`

Check your `package.json` if `strapi-provider-upload-aws-s3` exists. If so, you have successfully installed Strapi AWS S3 upload provider.

<b>Step 2 : Create plugins.js in config directory</b>
PATH : `./config/plugins.js`

```javascript
module.exports = ({ env }) => ({
 upload: {
   provider: 'aws-s3',
   providerOptions: {
     accessKeyId: env('AWS_ACCESS_KEY_ID'),
     secretAccessKey: env('AWS_ACCESS_SECRET'),
     region: env('AWS_REGION'),
     params: {
       Bucket: env('AWS_BUCKET_NAME'),
        StorageClass: env('AWS_S3_STORAGE_CLASSES') // Configure your Amazon S3 Storage Classes (including this to environment variable is not a must)
     },
     logger: console // Only if you want to `stdout` logs
   }
 }
});
```

Configuration
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hi7fru0zudt2eptn9gn.png)

Please refer to [constructor details](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property) for complete configuration.

You may be wondering what `AWS_S3_STORAGE_CLASSES` is. Here are Amazon S3 Storage Classes :
**S3 Standard**
- general-purpose storage of frequently accessed data

**S3 Intelligent-Tiering**
- data with unknown or changing access patterns

**S3 Standard-Infrequent Access (S3 Standard-IA)** and **S3 One Zone-Infrequent Access (S3 One Zone-IA)**
- for long-lived, but less frequently accessed data

**Amazon S3 Glacier (S3 Glacier)** and **Amazon S3 Glacier Deep Archive (S3 Glacier Deep Archive)**

- for long-term archive and digital preservation.

Read [Amazon S3 Storage Classes](https://aws.amazon.com/s3/storage-classes/) for more.

**Step 3 : Fill in environment variables**
In `./config/plugins.js` above, we assigned a lot of environment variables. Now it’s time to fill them in.
PATH : `.env` (root directory of your Strapi app)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9awqscvwkc9cxpewu1h.png)

<b>How do I gain my AWS security credentials ?</b>
Simply sign in to the AWS management console, then open the dropdown menu. Select My Security Credentials.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cf2il3l7d5w2f7nnra2b.jpg)

After that you will see a page like this :
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7swqkw1ydn29dftpnslv.jpg)

Learn more [how to gain your AWS security credentials](https://aws.amazon.com/blogs/security/how-to-find-update-access-keys-password-mfa-aws-management-console/).

Great! Everything is set, next you can start your Strapi app and upload files. Your files will be stored in AWS S3.

Thank you for reading, hope you have a great day today!

**Connect with me** :
[LinkedIn - kevinadhiguna](http://linkedin.com/in/kevinadhiguna)
[Github - kevinadhiguna](http://github.com/kevinadhiguna)

**References** :
- [Amazon Object Storage Classes](https://aws.amazon.com/s3/storage-classes/)
- [Strapi Amazon AWS](https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/amazon-aws.html)
- [AWS Javascript SDK](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
AWS Security Blog. https://aws.amazon.com/blogs/security/how-to-find-update-access-keys-password-mfa-aws-management-console/)