User menu
    • SnapiX About
    • SnapiX API Reference
    • SnapiX MCP Reference
    • SnapiX SDK Reference
    • SnapiX Plans
AU
By using this site you accept the terms of use, privacy policy and cookie policy.
App logo
Cover image
Published by Spas Z. Spasov. Last edit by Spas Z. Spasov on September 27, 2025

Managing AWS S3 via @aws-sdk/client-s3 in JavaScript

Cre­ate an IAM User with the nec­es­sary poli­cies and ac­cess keys to man­age AWS S3 via @aws-sdk/client-s3.

Table of Con­tents

  • Cre­ate Iden­ti­ty and Ac­cess Man­age­ment (IAM) User and as­sign the ap­pro­pri­ate poli­cies
  • Cre­ate spe­cif­ic IAM Pol­i­cy
  • As­sign Ac­cess key to the User
  • Use the Keys with AWS JavaScript SDK
  • Se­cu­ri­ty Best Prac­tices

Cre­ate Iden­ti­ty and Ac­cess Man­age­ment (IAM) User and as­sign the ap­pro­pri­ate poli­cies

1. Go to Ama­zon IAM > Users and Create user.

2. Step 1 Spec­i­fy user de­tails: Type the user name and do not pro­vide ac­cess to the AWS Man­age­ment Con­sole.

3. Step 2 Set per­mis­sions:

  • Per­mis­sions op­tions: At­tach poli­cies di­rect­ly,
  • Per­mis­sions poli­cies: click on Create policy - this will redi­rect you to IAM > Poli­cies > Cre­ate pol­i­cy screen (we will de­scribe it be­low),
  • Once the pol­i­cy is cre­at­ed go back to IAM > Users > Create user screen and re­fresh the pol­i­cy list,
  • Then search for the new­ly cre­at­ed pol­i­cy(s) and se­lect them,
  • Fi­nal­ly click on the next but­ton.

4. Step 3 Re­view and cre­ate: If every­thing looks good click on the cre­ate user but­ton.

5. Then pro­ceed with the last sec­tion

Cre­ate spe­cif­ic IAM Pol­i­cy

1. Step 1 Spec­i­fy per­mis­sions: Click on JSON and paste the fol­low­ing code in the pol­i­cy ed­i­tor:

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Sid": "AllowS3ObjectOperations",
			"Effect": "Allow",
			"Action": [
				"s3:PutObject",
				"s3:GetObject",
				"s3:DeleteObject"
			],
			"Resource": "arn:aws:s3:::bucket-test/*"
		},
		{
			"Sid": "AllowListingBucket",
			"Effect": "Allow",
			"Action": "s3:ListBucket",
			"Resource": "arn:aws:s3:::bucket-test"
		}
	]
}
  • Re­place bucket-test with the ac­tu­al buck­et name.

This pol­i­cy will al­low you to list the buck­et, up­load, mod­i­fy and delete ob­jects. If you want to al­low only the up­load op­tion, your pol­i­cy could looks like:

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"s3:PutObject",
				"s3:ListBucket"
			],
			"Resource": [
				"arn:aws:s3:::bucket-test",
				"arn:aws:s3:::bucket-test/*"
			]
		}
	]
}

2. Step 2 Re­view and cre­ate:

  • Name the pol­i­cy, note it is spe­cif­ic to a cer­tain buck­et and maybe it is a good idea to men­tion its name in the pol­i­cy name.
  • Fi­nal­ly click on the cre­ate pol­i­cy but­ton.

As­sign Ac­cess key to the User

1. Go to Ama­zon IAM > Users and se­lect the user from the users ta­ble. 2. Open the Se­cu­ri­ty cre­den­tials and with­in the sec­tion Ac­cess keys click on the Create access key but­ton. 3. On the Cre­ate ac­cess key screen:

  • Step 1 Ac­cess key best prac­tices & al­ter­na­tives: Ap­pli­ca­tion run­ning out­side AWS,
  • Step 2 Set de­scrip­tion tag: op­tion­al­ly write tag de­scrip­tion, and click on the Cre­ate ac­cess key but­ton.
  • Step 3 Re­trieve ac­cess keys: Click on the but­ton Down­load .csv file, copy and use your key - you are done. IM­POR­TANT!

Use the Keys with AWS JavaScript SDK

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import { readFile } from "fs/promises";

// NEVER hardcode credentials in production code
// Use environment variables or AWS credential providers instead
const s3Client = new S3Client({
  region: "eu-central-1",
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
  }
});

async function uploadToS3(filePath, key) {
  try {
    const fileContent = await readFile(filePath);
    
    const command = new PutObjectCommand({
      Bucket: "snapix-test-aws-s3.metalevel.cloud",
      Key: key,
      Body: fileContent
    });

    const response = await s3Client.send(command);
    console.log("Upload successful", response);
    return response;
  } catch (err) {
    console.error("Error uploading to S3:", err);
    throw err;
  }
}

// Usage
uploadToS3("./local-file.jpg", "uploads/my-image.jpg");

Se­cu­ri­ty Best Prac­tices

  1. Nev­er hard­code cre­den­tials in your ap­pli­ca­tion code
  2. Store cre­den­tials in en­vi­ron­ment vari­ables or use AWS cre­den­tial providers
  3. Ro­tate ac­cess keys reg­u­lar­ly (every 90 days rec­om­mend­ed)
  4. En­able MFA for the IAM user
  5. Con­sid­er us­ing tem­po­rary cre­den­tials via AWS STS for en­hanced se­cu­ri­ty
  6. Mon­i­tor ac­cess key us­age with AWS Cloud­Trail
  7. For web ap­pli­ca­tions, con­sid­er us­ing pre-signed URLs or Ama­zon Cog­ni­to in­stead of em­bed­ding ac­cess keys

Re­mem­ber that for ap­pli­ca­tions run­ning on AWS ser­vices like EC2, Lamb­da, or ECS, us­ing IAM roles is more se­cure than ac­cess keys.

Ref­er­ences:

  • Up­load files into a S3 since a ex­ter­nal Serv­er | AWS re

  • Use Cre­ateAc­cessKey with an AWS SDK or CLI - AWS Iden­ti­ty and Ac­cess Man­age­ment