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 :
- A Strapi app (Learn how to install Strapi)
- A package manager, such as Yarn or NPM
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
}
}
});
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)
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.
After that you will see a page like this :
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)
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.
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
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
Is it possible to use IAM role instead of IAM user access keys?
Hello,
Which permissions are needed to use the provider?
Thanks