AWS S3 with Nodejs practice

AWS SA Fundamentals

Object storage built to store and retrieve any amount of data from anywhere —


Use case: store object data

Pricing: 12 MONTHS FREE 5GB with Free Tier (new account), then $0.023 per GB for the First 50 TB / Month

Working with AWS S3

AWS SDK for JavaScript in Node.js

For example, I create a new project with some functions integrated with AWS S3. I like NodeJs + Typescript so that I decided to use NestJS’s template. It’s a nice approach and modern.

NestJS is a progressive Node.js framework for building efficient, reliable and scalable server-side applications —

About AWS Account for a developer, you should create new IAM user to get ACCESS_KEY_ID and SECRET_ACCESS_KEY

Go to IAM (Identity and Access Management) -> Users (under Access management) -> Add user

Then go next to the Permission step. Create a new Developer group attached AmazonS3FullAccess policy

Next to the last step and Create User

Save Access key ID and Secret access key to use in a project

Now install nestjs cli and create a new project or just clone my empty nestjs template (

$ npm i -g @nestjs/cli
$ nest new aws_example

Install nestjs configuration

$ npm i --save @nestjs/config# Update app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

imports: [ConfigModule.forRoot()],
export class AppModule {}

Then create new .env file and put the aws credentials

# Keep the same key, the aws auto load credentials

Install aws-sdk

$ npm i aws-sdk --save

Create a new service with nest cli

$ nest g s aws/s3

Then add some script to s3.service.ts

Update main.ts

import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';
import {S3Service} from "./aws/s3/s3.service";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const s3Service = app.get(S3Service);
const listBuckets = await s3Service.listBuckets();



Now run example

$ npm start

Now we try to test create a new bucket

const bucketName = 'empty-test00001';
const createBucket = await s3Service.createBucket(bucketName);

The output will create successfully with the bucketName is unique

{ createBucket: '' }

Try to upload an empty file

const uploadFile = await s3Service.uploadFile(bucketName, './res/nhancv.txt');

The success output

but the link it does not work well

We need to update the Bucket policy. You can be done on the website here. Default it’s empty

Just push content below

"Version": "2012-10-17",
"Statement": [
"Sid": "1583911202112",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::empty-test00001/*"

Another way is just to run a script directly

const putBucketPolicy = await s3Service.putBucketPolicy(bucketName);

now the link work correctly

Go to delete Bucket. The bucket must be empty in order to delete it.

const deleteObject = await s3Service.deleteObject(bucketName, 'nhancv.txt');
const deleteBucket = await s3Service.deleteBucket(bucketName);

The repository here, I will update more examples later.

Leave a Reply

Your email address will not be published.Required fields are marked *