How to upload files to S3 from Node.js

By

Learn how to upload files to AWS S3 from Node.js: create a bucket and an IAM user, store the keys in .env, then send the file with AWS SDK v3 S3Client and PutObjectCommand.

~~~

I had a simple use case.

I was building a job board and it was time to build the form to submit a new job offer.

The recruiter can enter the company details, the job details, and the company logo image.

The data is stored in the database, and at first I tried to store the logo in the database, but after a while I realized that while technically ok, I had some issues with storing the binary data and it was taking too long for the task. So I said, “ok let’s just upload it to S3”.

S3 is one of the wonderful services provided by AWS. Since I already use AWS for other things, adding a S3 bucket is easy.

So I went to create an S3 bucket.

I already had an AWS account. If you don’t, start here: https://aws.amazon.com.

Once you have an account set up, create a IAM user in AWS. Login to AWS and open the IAM console.

On the sidebar click “Users”, then “Create user”. Give it a name. This user does not need console access, we only want programmatic access.

In the permissions step, click “Attach policies directly”.

Type “S3” in the filter to show the S3 policies, and select the AmazonS3FullAccess permission.

Once the user is created, open it, go to the “Security credentials” tab and create an access key. You’ll get a pair of access key ID and secret access key. Copy those to your .env file in the project you have, or store them somewhere so you can use them later. AWS shows the secret only once. See also how to use environment variables in Node.js.

Next, go in S3 and create a bucket. From the S3 homepage https://s3.console.aws.amazon.com click the “Create bucket” button.

Set a name, choose an AWS region, disable “Block all public access” (we’ll get to permissions in another post) and click the Create bucket button at the bottom of the page.

Done! Now it’s time to dive into Node.js. I use this code in Next.js, server side, in an API call.

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 September 2025):

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

If you upload large files and want the multipart helper (the replacement for the old s3.upload()), also install @aws-sdk/lib-storage@3.

As mentioned, store your AWS access codes in .env:

AWS_ACCESS_KEY_ID=<the access key>
AWS_SECRET_ACCESS_KEY=<the secret>
AWS_BUCKET_NAME=<the bucket name>
AWS_REGION=<the region, e.g. eu-west-1>

Add

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

on top.

Then initialize the S3 client with:

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

Now when you want to upload a file, load it from the storage and send a PutObjectCommand:

const filename = 'the-file-name'
const fileContent = fs.readFileSync(`${filename}.jpg`)
const key = `${filename}.jpg`

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

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

PutObjectCommand does not return a Location field the way the old v2 s3.upload() did. You build the public URL yourself (or keep the key and serve it through CloudFront).

For bigger files, swap the send call for the multipart helper:

import { Upload } from '@aws-sdk/lib-storage'

const upload = new Upload({
  client: s3,
  params: {
    Bucket: process.env.AWS_BUCKET_NAME,
    Key: key,
    Body: fileContent,
  },
})

const data = await upload.done()
// data.Location is the public URL

I also have a shorter companion post on uploading an image to S3 with Node.js if you only need the image case.

Tagged: Node.js · All topics

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

~~~

Related posts about node: