Using Amazon S3 Bucket with Node.js: A Step-by-Step Guide

Amazon S3 (Simple Storage Service) is a popular object storage service offered by Amazon Web Services (AWS) that allows you to store and retrieve any amount of data from anywhere on the web. In this guide, we will walk you through the process of integrating Amazon S3 Bucket with Node.js to handle file uploads and downloads.

Prerequisites

Before you begin, make sure you have the following:

  1. An AWS account with access to S3 services.
  2. Node.js and npm (Node Package Manager) are installed on your machine.

Step 1: Set Up AWS Credentials

To interact with AWS services, you’ll need to configure your AWS credentials on your development environment. You can do this by using the AWS CLI or by manually creating a ~/.aws/credentials file. Replace YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your actual AWS access and secret keys.

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

Step 2: Create an S3 Bucket

Log in to your AWS console and navigate to the S3 service. Create a new bucket by clicking the “Create bucket” button. Follow the prompts to choose a unique bucket name and configure other settings.

Step 3: Set Up Your Node.js Project

Create a new Node.js project (or use an existing one) and install the AWS SDK using npm:

npm install aws-sdk

Step 4: Use AWS SDK to Interact with S3

Upload a File to S3

In your Node.js file, require the AWS SDK and create a new instance of the S3 service:

const AWS = require('aws-sdk');
const fs = require('fs');

// Configure AWS SDK
AWS.config.update({region: 'us-east-1'}); // Change to your desired region

const s3 = new AWS.S3();

// Define the parameters for the upload
const params = {
  Bucket: 'your-bucket-name',
  Key: 'example.jpg', // Name of the file in S3
  Body: fs.readFileSync('path/to/local/file.jpg') // Local file to upload
};

// Upload the file
s3.upload(params, (err, data) => {
  if (err) throw err;
  console.log('File uploaded successfully.', data.Location);
});

Download a File from S3

To download a file from S3, use the getObject method:

const params = {
  Bucket: 'your-bucket-name',
  Key: 'example.jpg' // Name of the file in S3
};

s3.getObject(params, (err, data) => {
  if (err) throw err;
  fs.writeFileSync('downloaded.jpg', data.Body);
  console.log('File downloaded successfully.');
});

Step 5: Test Your Code

Run your Node.js file using node your-file.js and ensure that the file is uploaded and downloaded successfully.

Congratulations! You’ve successfully used Node.js to interact with Amazon S3 Bucket. You can now build applications that utilize S3 for file storage and retrieval.

Remember to manage your AWS credentials and access permissions carefully to ensure the security of your S3 resources. Happy coding!

Don’t forget to check other posts on AWS ๐Ÿ˜‰

spring-boot-hateoas-for-restful-services

Spring Boot – HATEOAS for RESTful Services

In this article, I will explain implementing HATEOAS for a REST API with Spring Boot with an example.

What is HATEOAS?

HATEOAS stands for โ€œHypermedia as the engine of application stateโ€

It’s a complicated acronym. Let me decode it for you.

What do you visualize when you visit a web page?

The data that you would want to see. Is that all? You would also see links and buttons to see related data.

For example, if you go to a product page, you will see

  • product details
  • Links to Edit and Delete product details
  • Links to see relevant products in that category

HATEOAS brings the exact same concepts to RESTful Web Services.

When some details of a resource are requested, you will provide the resource details as well as details of related resources and the possible actions you can perform on the resource. For example, when requesting information about a Facebook user, a REST service can return the following

  • User details
  • Links to get his recent comments
  • Links to retrieve his friend list
  • Links to get his recent posts

Sigh, a lot of things to read right? Let’s, get our hands dirty =D

Create the project

Create a new project using start.spring.io

Add basic CRUD Operation

I’m not gonna explain CRUD operation in detail, you can refer to my project uploaded on Github.

Enhancing the resource

To implement HATEOAS, we would need to include related resources in the response. Instead of Product, I’ll use a return type of EntityModel<Product>. EntityModel is a simple class wrapping a domain object and allows adding links to it.

@GetMapping("/{id}")
public EntityModel<Product> getProduct(@PathVariable long id) {
    Optional<Product> product = productRepository.getProductById(id);

    if (!product.isPresent())
        throw new ProductNotFoundException("id-" + id);
    EntityModel<Product> resource = EntityModel.of(product.get());
    WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).getAllProducts());
    resource.add(linkTo.withRel("all-products"));
    return resource;
}

So what’s happening here actually?

EntityModel<Product> resource = EntityModel.of(product.get());

In the above line, we are creating a new resource. Then, adding the link to retrieve all products method to the link.

WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).getAllProducts());
resource.add(linkTo.withRel("all-products"));

In the above line, we are creating a new resource. After making the changes so far, let’s start the service and see the change in action.

{
    “id”: 1,
    “name”: “Dell Precision 5550”,
    “currentStock”: 99,
    “description”: “The world’s smallest and thinnest 15 inch mobile workstation.”,
    “type”: “Laptop”,
    “dateCreated”: “2022-10-23T08:33:10.658+00:00”,
    “lastModified”: “2022-10-23T08:33:10.658+00:00”,
    “_links”: {
        “all-products”: {
            “href”: “http://localhost:3001/api/v1/product”
        }
    }
}

Don’t worry, I’ve added a bootstrap class to add a few products into the DB whenever the application starts.

The above example covers important concepts in enhancing resources with HATEOAS.

However, ain’t gonna make this post longer. You have to make the important decisions:

  • What are the important resources related to a specific resource?

Think of this as homework, go ahead and enhance the application with more HATEOAS links.

Oh! I forgot to give you the Github link, Here you go: https://github.com/nazrul-kabir/spring-boot-rest-service-with-hateoas Feel free to comment on the post if you have any comments.

And, don’t forget to read my other posts on Spring Boot

A Deep Dive into CSS Grid

As the title says A Deep Dive into CSS Grid, I’m not gonna explain the basic of CSS grid rather in this post I’ll try to explore almost all features and properties of CSS grid.

Recap: CSS grid allows us to divide a webpage into rows and columns with simple properties to build complex layouts as well as small user interfaces using CSS only, without any change to the HTML.

Let’s dive in detail..

Property: display
To make an element a grid, set its display property to grid.

.to-be-grid {
display: grid;
}

Doing this makes .to-be-grid a grid container and its children grid items.

Property: grid-template-columns

We can create columns by using the grid-template-columns property. To define columns set this grid-template-columns property to column sizes in the order that you want them in the grid to appear. Let’s have a look:

.grid {
display: grid;
grid-template-columns: 100px 100px 100px;
}

This defines 3 100px width columns. All grid items will be arranged in order, in these columns. And the row height will be equal to the height of the tallest element in row but this also can be changed with grid-template-rows.

Property: grid-template-rows

It is used to define the number & the size of rows in a grid. It is similar in syntax to grid-template-rows.

.grid {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}

Only the grid-template-rows property without grid-template-columns would result in column width being same as the width of the widest element in that row.

Property: grid-template

Grid is shorthand for three properties: grid-template-rows, grid-template-columns, and grid-template-areas.

You can use it is like this:

.grid {
grid-template:
“header header header” 80px
“nav article article” 600px
/ 100px 1fr;
}

You define the template areas such as you normally would, place the width of every row on its side then finally place all the column widths after a forward slash. Like before, you’ll place all of it on one line.

Data Type:

The unit “fr” is a newly created unit for CSS Grid Layout. The fr unit helps to create flexible grids without calculating percentages. 1 fr represents one fraction of available space. The available space is divided into total number of fractions defined. So, 3fr 4fr 3fr divides the space into 4+3+4=11 parts and allocates 4, 3 and 4 parts of the available space for the three rows/columns respectively. For example:

.grid {
display: grid;
grid-template-columns: 3fr 4fr 3fr;
}

If you combine fixed units with flexible units, the available space for the fractions is calculated after subtracting the fixed space. Let’s check out another example:

.grid {
display: grid;
grid-template-columns: 3fr 200px 3fr;
}

The width of a single fraction is calculated like: (width of .grid – 200px) / (3 + 3). The space of the gutters, if any would have also been subtracted initially from the width of .grid. This is the difference between frs and %s that percentages don’t include the gutter that you define with grid-gap.

Here 3fr 200px 3fr is essentially equal to 1fr 200px 1fr.

Guess, that’s a lot for today :pย  Will continue remaining in part two.

C# join tables using IQueryable in LINQ

Yesterday I was struggling with how to Join tables using IQueryable in C# or Asp.Net and return it in List. So, here I’m with the solution. Let’s start..

With the introduction of LINQ the difference between writing code for  accessing a list of data in an external data source like SQL Server is vanishing. With the introduction of .NET Framework 4.0 this has changed.In this post i would like to filter my SQL data employing 2 tables.

In my project I’ve 2 tables. 1: Containing all the basic user data like Full Name, DOB etc and 2nd containing user’s student id, card number and so on.. I’ve needed a Data like this: Full Name [Roll Number]

So, here’s the code

var query = (from a in DbInstance.User_Account
join s in DbInstance.User_Student on a.Id equals s.Id
where s.Status == 1
select new { a, s } into t1
select new
{
Id = t1.a.Id.ToString(),
Name = string.Concat(t1.a.FullName.ToString(), ” [“, t1.s.RollNumber.ToString(), “]”)
}).ToList();

Convert currentTimeMillis to a datetime in JAVA(Spring Boot)

Spring Boot

In this tutorial I’ll show how to save Time Stamp in Database in currentTimeMillis and retrieve in Spring Boot

Want precise time for some purpose? Normally, we can save Time Stamp in Database but what if the data need to be converted into several formats?

Let’s say we need to save “2019-6-27 00:59:59” and then convert it to several formats like ’27/2019/6 00:59:59′ and ’20-JUN-1990 08:03:00′. Though “2019-6-27 00:59:59” can be converted but that’s a little bit tough. The ideal solution is to save the data in millisecond format. In millisecond format “2019-6-27 00:59:59” would be “1564167599000”

I assume you already have the datetime picker data in controller, here’s the conversion code:

String expireTime = model.attribute; //your model attribute or request parameter name
Date date = formatter.parse(expireTime);
Timestamp timestamp = new Timestamp(date.getTime());
String expireTime2 = String.valueOf(timestamp.getTime());
//That's it! We've converted '2019-6-27 00:59:59' to '1564167599000'
//Add your save logic

Now the reading part. User can’t read ‘1564167599000’, right? Let’s convert this to readable format:

String convertedExpireTime = "1564167599000";
//Create a calendar instance
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(convertedExpireTime));
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMin = calendar.get(Calendar.MINUTE);
int mSec = calendar.get(Calendar.SECOND);
String readableFormat = mYear + "-" + mMonth + "-" + mDay + " " + mHour + ":"+ mMin + ":"+ mSec; // The value is now converted to 2019-6-27 0:59:59
//Add some logic

That’s all! We’re done!

If you’ve any confusion please let me know ๐Ÿ™‚

How to query data out of the box using Spring data JPA by both Sort and Pageable

Spring Boot

Developing REST API? Clients wants both sort & pagination?

That’s quite simple btw!

Since last few hours was trying to add this simple feature my Spring Boot project. In my project pagination was done using pageable

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

public List<Model> getAllData(Pageable pageable){
       List<Model> models= new ArrayList<>();
       modelRepository.findAllByOrderByIdDesc(pageable).forEach(models::add);
       return models;
   }
//Model = Your Model where you want to implement this feature.ย 

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to display multiple table contents in Spring Boot project using Thymeleaf, JPA Entity and Model.

In my project I have mapped a relationship between the two entities I need to retrieve data from and am wondering how to display data from the two tables on list page. I’ve ‘category’ table and ‘product’ table like below:

Spring Boot, Thymeleaf Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

And in my controller I’ve list of both category and product in respective model. Here is the snippet:

model.addAttribute("products", productService.findAll());
model.addAttribute("categories", categoryService.findAll());

So, I’ve all the required data in the model and I can access them from Thymeleaf. Let’s copy the table code from below (N.B: I’ve used bootstrap table):

<table class="table table-striped table-bordered table-responsive" role="grid">
   <thead>
      <th>Category</th>
      <th>Product</th>
   </thead>
   <tbody>
      <tr role="row" class="odd" th:each="product:${products}">
         <td>
            <p th:each="category : ${categories}" th:if="(${category.id} == ${product.id})" th:text="${category.name}"></p>
         </td>
         <td th:text="${product.name}">
      </tr>
   </tbody>
</table>

It’ll produce something like this:

Spring Boot, Thymeleaf: Display data from two JPA entity tables on same page

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Thymeleaf select required not working?

thymeleaf

Since last few days trying to add select “required” attribute in my Thymeleaf project.  Neither of these works in Thymeleaf:

  • <select required=”required”>
  • <select required=’required’>
  • <select required=required>
  • <select required=””>
  • <select required=”>
  • <select required>

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

<select th:name="someName" class="form-control" id="someID" th:required="true">
    <option value="">Select Category</option>
    <option th:each="model: ${someModel}" th:value="${model.id}"
            th:text="${model.Title}"></option>
</select>

Please note that, first/default option value must be set to “” if you put “0” it won’t work!

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to dynamically create modal popup in your Spring Boot project using Twitter Bootstrap 4 to show the user a ‘warning popup’ before deleting a data or edit the contents on the fly in same page. Not to mention I’ve used Thymeleaf as my template engine.

In this tutorial I’m gonna show how to achieve this.

I assume you have already included required Bootstrap resources and necessary jQuery library in your project and are ready to implement modal popup. Let’s copy the modal code from below:

<div class="modal modal-warning fade in" id="myModal">
 <div class="modal-dialog">
 <div class="modal-content">
 <div class="modal-header">
 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
 <span aria-hidden="true">ร—</span></button>
 <h5 class="modal-title">Delete User</h5>
 </div>
 <div class="modal-body">
 <h3>Are you sure want to delete this user?</h3>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
 <a type="button" class="btn btn-danger"><i class="fa fa-check"></i>&nbsp;Yes</a>
 </div>
 </div>
 </div>
 </div>

It’ll produce something like this:

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

Now, the next step actually the final step. Make the modal dynamically generate for all the data in table.

I’ve some data in the table like below now I want to delete a user but before that I want that modal to generate dynamically for all data.

Bootstrap Table in Spring boot

On the ‘Red’ delete button the popup will generate.

<a data-toggle="modal" data-target="#modal-warning" th:attr="data-target='#modal-warning'+${user.id }"><span class="glyphicon glyphicon-trash"></span></a>

Here “${user.id}” is my thymeleaf object.

Now, let’s make the modal dynamic with the id:

<div class="modal modal-warning fade in" th:id="modal-warning+${user.id }" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">ร—</span></button>
                <h5 class="modal-title">Delete User</h5>
            </div>
            <div class="modal-body">
                <h3>Are you sure want to delete this user?</h3>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
                <a type="button" class="btn btn-outline" th:href="@{/user/delete/{id}(id=${user.id})}"><i class="fa fa-check"></i>&nbsp;Yes</a>
            </div>
        </div>
    </div>
</div>

Here “th:href=”@{/user/delete/{id}(id=${user.id})}” is your POST URL.
You can also edit any data inside the modal by adding a form.

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚