Amazon S3 — Developing with AWS SDK to interact Serverless APIs

In this article, we are going to develop Amazon S3 interactions with using AWS SDK.

Amazon S3 Walkthrough with AWS SDK JS V3

We are going to do Amazon S3 SDK Examples with using AWS SDK JavaScript v3 and use ES6 standards. By the end of the article, we will invoke S3 API to perform bucket related tasks using the latest AWS SDK.

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

Amazon S3— Developing with AWS SDK

Amazon Simple Storage Service (Amazon S3) is a web service that provides highly scalable cloud storage. Amazon S3 provides easy to use object storage, with a simple web service interface to store and retrieve any amount of data from anywhere on the web.

Lets talk about the topics that we will develop in this section:

  • Creating and using Amazon S3 buckets
  • Configuring Amazon S3 buckets
  • Managing Amazon S3 bucket access permissions
  • Working with Amazon S3 bucket policies
  • Using an Amazon S3 bucket as a static web host

In order to perform these operations with SDK, Amazon S3 has package that we can see on Reference documents. Reference documents and need to use S3 packages.

You can see Commands section of S3. Clients — Commands to interact with SDK packages. As you remember that v3 has modularized structure and send commands with client objects.

This will reduce code size and increase lambda executions performance. As you can see that we have understood Amazon S3 SDK libraries and start Developing with AWS SDK for Programmatic Access w/ Serverless APIs. But before that its good to understand S3 APIs which APIs exposing from S3 and which interactions can be held by SDK using S3 core APIs.

Understanding Amazon S3 Interactions — Working with Buckets and Objects

Before we developing NodeJS lambda functions, We should understand the basics of S3, after that it will be very clear to interact with S3 from our NodeJS lambda function. So the idea is that you should think S3 as a AWS resource and S3 expose awaitable APIs that we can call from our lambda function with using AWS SDK.

The Amazon S3 APIs are grouped into three sets: Amazon Simple Storage Service, AWS S3 Control, and Amazon S3 on Outposts. There is no functional distinction between the three sets.

In general, APIs that apply bucket- and object-level actions are in the Amazon Simple Storage Service set, and APIs that apply account-level actions are in the AWS S3 Control set. With Amazon S3 on Outposts, you can create S3 buckets on AWS Outposts and easily store and retrieve objects on premises.

See all actions of S3

So after we have learned the basics of S3, how we can interact these methods from lambda function with using AWS SDK ? of course using with AWS SDK for JavaScript — Developer Guide for SDK Version 3.

Create NodeJS Project with DynamoDB SDK Packages

We are going to Create NodeJS Project with S3 SDK Packages to interact with S3 APIs. Open VS Code, create “s3-sdk” folder. This will be our NodeJS project for this section.

Locate folder and run npm command in order to create package.json:

npm init -y

See and edit default package.json is created. The first thing we should do
Add “type”: “module”:

package.json 
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“type”: “module” — — — ADDED
}

this provide to use ES6 codes into nodejs projects. for example “import statements”.

Install required S3 sdk packages

Run command on package.json level

npm install @aws-sdk/client-s3

See package installed in package.json file: “@aws-sdk/client-s3”

{
“name”: “section8”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“type”: “module”,
“dependencies”: {
@aws-sdk/client-s3”: “³.87.0”
}
}

As you can see that node_modules are generated when running npm install command.

Create S3Client NodeJS Module For Connecting DynamoDB

This section we will interact with S3. For all these interactions we need to connect S3, for that purpose we should establish S3Client object. Instead of creating new client for every SDK request, its best practice to create NodeJS module and use this module for every particular SDK commands.

Amazon S3 Walkthrough with AWS SDK JS V3

Create a libs directory, and create a Node.js module with the file name s3Client.js. Copy and paste the code below into it, which creates the S3 client object. Replace REGION with your AWS region.

// Create service client module using ES6 syntax.
import { S3Client } from “@aws-sdk/client-s3”;
// Set the AWS Region.
const REGION = “REGION”; //e.g. “us-east-2”
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });
export { s3Client };

As you can see that we have created NodeJS module for S3 connections with creating a new instance of S3Client object. This client object will use every interaction with our SDK examples.

Now we are ready to develop our Topics:

  • Creating and using Amazon S3 buckets
  • Configuring Amazon S3 buckets
  • Managing Amazon S3 bucket access permissions
  • Working with Amazon S3 bucket policies
  • Using an Amazon S3 bucket as a static web host

Creating and using Amazon S3 buckets with AWS SDK Commands Classes

In this example, a series of Node.js modules are used to obtain a list of existing Amazon S3 buckets, create a bucket, and upload a file to a specified bucket. These Node.js modules use the SDK for JavaScript to get information from and upload files to an Amazon S3 bucket using these methods of the Amazon S3 client class:

  • ListBucketsCommand
  • CreateBucketCommand
  • ListObjectsCommand
  • PutObjectCommand
  • UploadPartCommand,
  • GetObjectCommand
  • DeleteBucketCommand

Listing Amazon S3 bucket with AWS SDK

Create a Node.js module with the file name s3_listbuckets.js. Make sure to configure the SDK as previously shown, including installing the required clients and packages. To access Amazon S3, create an S3 client service object. Call the listBuckets method of the Amazon S3 client service object to retrieve a list of your buckets.

// Import required AWS SDK clients and commands for Node.js.
import { ListBucketsCommand } from “@aws-sdk/client-s3”;
import { s3Client } from “./libs/s3Client.js”; // Helper function that creates an Amazon S3 service client module.
import { ListBucketsCommand } from “@aws-sdk/client-s3”;
import { s3Client } from “./s3client.js”;
export const run = async () => {
try {
const data = await s3Client.send(new ListBucketsCommand({}));
console.log(“Success”, data.Buckets);
} catch (err) {
console.log(“Error”, err);
}
};
run();

To run the example, enter the following at the command prompt.

node s3_listbuckets.js

Creating an Amazon S3 Bucket with AWS SDK

Create a Node.js module with the file name s3_createbucket.js. Add a variable to hold the parameters used to call the createBucket method of the Amazon S3 client service object, including the name for the newly created bucket.

// Get service clients module and commands using ES6 syntax.
import { CreateBucketCommand } from “@aws-sdk/client-s3”;
import { s3 } from “./libs/s3Client.js”;
// Set the bucket parameters
const bucketParams = { Bucket: “BUCKET_NAME” };
// Create the Amazon S3 bucket.
const run = async () => {
try {
const data = await s3.send(new CreateBucketCommand(bucketParams));
console.log(“Success”, data.Location);
return data;
} catch (err) {
console.log(“Error”, err);
}
};
run();

See that we have only 1 table — order. I am going to skip other commands but if you would like to continue, you can follow the below course.

Step by Step Design AWS Architectures w/ Course

I have just published a new course — AWS Lambda & Serverless — Developer Guide with Hands-on Labs.

In this course, we will learn almost all the AWS Serverless Services with all aspects. We are going to build serverless applications with using AWS Lambda, Amazon API Gateway, Amazon DynamoDB, Amazon Cognito, Amazon S3, Amazon SNS, Amazon SQS, Amazon EventBridge, AWS Step Functions, DynamoDB and Kinesis Streams. This course will be 100% hands-on, and you will be developing a real-world application with hands-on labs together and step by step.

Source Code

Get the Source Code from Serverless Microservices GitHub — Clone or fork this repository, if you like don’t forget the star. If you find or ask anything you can directly open issue on repository.

--

--

Mehmet Ozkaya
AWS Lambda & Serverless — Developer Guide with Hands-on Labs

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya