AWS S3 with Nodejs practice

AWS SA Fundamentals

Object storage built to store and retrieve any amount of data from anywhere — https://aws.amazon.com/s3/

Definitionhttps://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html

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 https://aws.amazon.com/s3/pricing/

Working with AWS S3https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html


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 — https://nestjs.com

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 (https://github.com/nhancv/nc-nestjs-template)

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

Install nestjs configuration

https://docs.nestjs.com/techniques/configuration

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

@Module({
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
# https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#sourcing-credentials-from-external-processes
AWS_REGION=us-east-2
AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

https://gist.github.com/nhancv/87ac207801b0844f431d8d3f31f5751e

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();
console.log({listBuckets});

}

bootstrap();

Now run example

$ npm start

Now we try to test create a new bucket

const bucketName = 'empty-test00001';
const createBucket = await s3Service.createBucket(bucketName);
console.log({createBucket});

The output will create successfully with the bucketName is unique

{ createBucket: 'http://empty-test00001.s3.amazonaws.com/' }

Try to upload an empty file

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

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);
console.log({putBucketPolicy});

now the link https://empty-test00001.s3.us-east-2.amazonaws.com/nhancv.txt work correctly

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

const deleteObject = await s3Service.deleteObject(bucketName, 'nhancv.txt');
console.log({deleteObject});
const deleteBucket = await s3Service.deleteBucket(bucketName);
console.log({deleteBucket});

The repository here, I will update more examples later.

https://github.com/nhancv/nc-aws

https://medium.com/@nhancv/aws-s3-with-nodejs-practice-91d2654d3d34

Leave a Reply

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