Vendure - Assets to an S3 bucket

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

As my webshop serves a lot of images, I wanted to leverage a good CDN and quickly found a neat integration with S3 available for Vendure.
In this article, I'll show you how you can connect and leverage an S3 bucket as your asset storage.
The first thing you'll need is an AWS account, which can be tedious. (Mainly quite long)
Head over to AWS and create your account.
Once you are set up and ready to go, it's time to create a new bucket. The steps for creating a bucket are extensive and might change over time, so it's best to follow Amazon's docs on this.
Eventually, your bucket should be ready like this:

You'll also need to add new access credentials.
Head to AWS's IAM section and create a new user.
You can give it a name that represents the tool you will use and also choose to make it an Access key.

On the next screen, you'll need to attach the permissions. In our case, we can choose to connect them manually.
Search for AmazonS3FullAccess.

You can press next until you get to the actual keys in the next couple of steps. Copy the key and secret to a secure place.
Now it's time to add some code.
First of all, we need to install the aws-sdk package.
npm install aws-sdk
I save my secrets in a .ENV file for maximum protection, so go ahead and add the following two keys.
AWS_ACCESS_KEY_ID={YOUR_KEY}
AWS_SECRET_ACCESS_KEY={YOUR_SECRET}
Next, we need to modify the vendure-config.ts file to use a new asset plugin.
Start by importing the S3 one.
import {
AssetServerPlugin,
configureS3AssetStorage,
} from '@vendure/asset-server-plugin';
And inside the config, add a new plugin.
export const config: VendureConfig = {
# Your other config here
plugins: [
AssetServerPlugin.init({
route: 'assets',
storageStrategyFactory: configureS3AssetStorage({
bucket: 'vendure',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
}),
}),
],
};
It's essential to change the bucket name to your bucket.
And now, any assets you upload via the admin UI or the import will be stored in the S3 bucket!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter