Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now

Nodejs resize s3 image using Sharp image processing library

Nodejs resize s3 image using Sharp image processing library

Sharp High performance Node.js image processing library. 

It is 4x to 5x faster then ImageMagick and GraphicsMagick.

We can convert any larger image to any common format such as png, gif, jpeg, webp.

Also we can modify image dimension on the go without modifying actual image.


Any systems running Node.js v10+ do not need any additional install or runtime dependencies.

to install sharp run the following command to your terminal

npm install sharp

S3 Integration

first we have to import sharp class using 

import sharp from 'sharp'; 

Now we will make s3 helper file to retrieve the file from aws s3.

to install aws sdk to your node js application run the following command

npm install aws-sdk

in class constructor  we will add following code

our s3 class constructor is successfully configured. now we will add a function to retrieve file from s3.

we will use the below code as follows

complete code of S3Helper class will be like this

Sharp image resize

Lets say we have a image file stored in s3, key name of that file is nature.png.

Actual size of the image is 1024x600. we want to convert to 200x200 size.

at first we will take the image extension. to do this we will use JavaScript match function.

to define image quality, we can pass the quality value from 0 to 100 to sharp object. we will define the quality into a variable

let quality = 100;

now we will fetch file from s3 using below code. Don't forget to load s3 helper class into your code.

let file: any = await this.s3Helper.getFile(key);

final part to convert image to our desired size, we can use below code to do this.

now console.log(data.toString('base64')), you will get converted image in base64 format.

if you want to save that file to disk, use below code

To know more please refer the following url 


Tags : AWS, javascript, Nodejs, S3,

Views : 1450

Subscribe Us

Follow Us