How can I upload the file to object storage with Node.js?

 

Going with cloud server is one of the best options where you will find a lot of advantages and also helps your business to grow.  Cloud is the latest form of technology where you can easily rely upon. And today a lot of companies are using the cloud hosting services for their business purpose. So what is cloud server? It is a virtual server which is running in a cloud technology environment. Today cloud hosting can be used for all types of applications, and not only for business security. In cloud server hosting it provides a scalable and flexible network solution and enable great opportunities to bring the new challenges.

 

But today if you are using a cloud server hosting for your website and if you are looking for information on how to upload the file to object storage with Node.js on a Cloud server, then here is the complete solution for it.  Today object storage is a popular and scalable method of storing and serving static assets for audio, images, text, PDFs, and other types of unstructured data. And today in cloud server it offers object storage in addition to traditional local or blocks storage, where it is used to store dynamic application files and databases.

 

Here are the steps to follow to upload the file to object storage in Node.js hosting on a Cloud server:

 

>> Log into SSH/Root

 

>> Install Node.js Dependencies and you have to create a directory to place your Node.js app and navigate to the directory.

 

mkdir sites/spaces-node-app && cd sites/spaces-node-app

 

>>  Now create a new package.json file for your project and paste the below command

 

package.json

{

 "name": "spaces-node-app",

 "version": "1.0.0",

 "main": "server.js",

 "scripts": {

   "start": "node server.js"

 },

 "license": "MIT"

}

 

>> Now you have to create the front end application by using the below command

 

<!DOCTYPE html>

<html lang="en">

 

<head>

 <meta charset="utf-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0">

 

 <title>HostingRaja Tutorial</title>

 

 <link rel="stylesheet" href="/./style.css">

</head>

 

<body>

 

 <!-- contents will go here -->

 

</body>

 

</html>

 

>> Now you can also write an error message in the body of error.html.

 

error.html

...

 

<h1>Something went wrong!</h1>

<p>File was not uploaded successfully.</p>

 

...

 

>>  Write a success message in the body of success.html. using the below command

 

success.html

...

 

<h1>Success!</h1>

<p>File uploaded successfully.</p>

 

...

 

>> So now in the index.html, you will create an HTML  form with multipart/form-data. To do so follow the below command

 

index.html

...

 

<h1>HostingRaja Tutorial</h1>

 

<p>Please select a file and submit the form to upload an asset to your HostingRaja Space.</p>

 

<form method="post" enctype="multipart/form-data" action="/upload">

 <label for="file">Upload a file</label>

 <input type="file" name="upload">

 <input type="submit" class="button">

</form>

 

...

 

>> Now you need to create style.css and add just enough CSS to make the application easy to read by adding the below command

 

style.css

html {

 font-family: sans-serif;

 line-height: 1.5;

 color: #333;

}

 

body {

 margin: 0 auto;

 max-width: 500px;

}

 

label,

input {

 display: block;

 margin: 5px 0;

}

 

>> Set Up an Express Server Environment, follow the below command

 

server.js

// Load dependencies

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

const express = require('express');

const multer = require('multer');

const multerS3 = require('multer-s3');

 

const app = express();

 

>> No you can route index.html, success.html, and error.html relative to the root of the server.

 

server.js

...

 

// Main, error and success views

app.get('/', function (request, response) {

 response.sendFile(__dirname + '/public/index.html');

});

 

app.get("/success", function (request, response) {

 response.sendFile(__dirname + '/public/success.html');

});

 

app.get("/error", function (request, response) {

 response.sendFile(__dirname + '/public/error.html');

});

 

>> Now you have to set the port on your server, so follow the below command

 

server.js

...

 

app.listen(3001, function () {

 console.log('Server listening on port 3001.');

});

 

>> Now you have to save the server.js and start the server.

 

npm start

Output

> node server.js

 

Server listening on port 3001.