DEV Community

Cover image for How to setup Amazon S3 upload provider in your Strapi app
kevinadhiguna
kevinadhiguna

Posted on • Edited on

How to setup Amazon S3 upload provider in your Strapi app

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.

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

Prerequisites :

Step 1 : Install Strapi AWS S3 upload provider
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.

Step 2 : Create plugins.js in config directory
PATH : ./config/plugins.js

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
   }
 }
});
Enter fullscreen mode Exit fullscreen mode

Configuration
Alt Text

Please refer to constructor details 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 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

How do I gain my AWS security credentials ?
Simply sign in to the AWS management console, then open the dropdown menu. Select My Security Credentials.
Alt Text

After that you will see a page like this :
Alt Text

Learn more how to gain your AWS security credentials.

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
Github - kevinadhiguna

References :

Top comments (5)

Collapse
 
xw1097 profile image
xw1097

folks, you should go to IAM role and create a specific IAM role(which includes access_key_id and secret) for strapi and create proper policy for it. if you're unsure you should learn IAM 101 somewhere, exposing root user access key and credential is risky business.

Collapse
 
pugalenthis profile image
PUGALENTHI-S

Hey hi I did all these steps but I will not able to upload the image to s3.How to confirm that my strapi application and s3 connected together

Collapse
 
hampton1122 profile image
Chris Hampton

It would be nice if this plugin allowed you to set a serviceURL instead of using AWS S3...we used Storage Grid which is an implementation of AWS S3

Collapse
 
csarigumba profile image
Cedric Sarigumba

Is it possible to use IAM role instead of IAM user access keys?

Collapse
 
44db profile image
Yannis Kassotis

Hello,
Which permissions are needed to use the provider?
Thanks