How to upload an image to S3 using Node.js

By

Learn how to upload an image to AWS S3 from Node.js with AWS SDK v3, creating an S3Client, sending a PutObjectCommand, and reading the image public URL.

~~~

In this post I want to share how to upload an image to AWS S3, the wonderful cloud file hosting solution provided by Amazon Web Services.

I already wrote about this topic in the past in how to upload files to S3 from Node.js

First, install the S3 client from the AWS SDK for JavaScript v3 (3.1134.0 as of September 2026). The old aws-sdk v2 package reached end of support in 2025, so use v3:

npm install @aws-sdk/client-s3@3

Import it in your code at the top of the file you’re going to add this file upload to S3 functionality:

import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
import fs from 'fs'

Next, use the SDK to create an instance of the S3 client. I assign it to a s3 variable:

const s3 = new S3Client({
  region: process.env.AWS_REGION,
  credentials: {
    accessKeyId: process.env.AWS_S3_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_S3_SECRET_ACCESS_KEY,
  },
})

Note that I use environment variables here: AWS_S3_ACCESS_KEY_ID, AWS_S3_SECRET_ACCESS_KEY, and AWS_REGION.

Now comes some “administrative work”. You need to create an IAM profile on AWS (the credentials) with programmatic access with the permissions for AmazonS3FullAccess and an S3 bucket that this user has access to.

I won’t cover this aspect here as you can find tons of articles and documentation about this. I’ll just talk about the JavaScript code you need.

Now, you need an image blob to upload.

You can use a URL like this:

const imageURL = 'https://url-to-image.jpg'
const res = await fetch(imageURL)
const blob = Buffer.from(await res.arrayBuffer())

or you can get an image sent from a form image field upload in a multipart form:

const imagePath = req.files[0].path
const blob = fs.readFileSync(imagePath)

Finally, send a PutObjectCommand and await the result:

const key = req.files[0].originalFilename

await s3.send(
  new PutObjectCommand({
    Bucket: process.env.AWS_S3_BUCKET_NAME,
    Key: key,
    Body: blob,
  })
)

const location = `https://${process.env.AWS_S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`

AWS_S3_BUCKET_NAME is the name of the S3 bucket, another environment variable

location is the public URL of the uploaded image on S3.

You have to make sure you set the S3 bucket as public so you can access that image URL.

Tagged: Node.js · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about node: