Building a Node.js application:

The updated code snippet documented below. Please stay tuned for updates...

In the future post, I will go through step by step instructions to build a database driven Node.js application that will be deployed on the K8s infra.

For now, I am sharing here some code snippets I have tested for the backend app to upload video files to S3. You can use this code for quick test of S3 availability. Note that I am using GridFS to store image files to MongoDB which is a versatile storage system suited to handling large files such as those exceeding the 16 MB document size limit. Ref.: https://docs.mongodb.com/manual/core/gridfs/

What this web applciation will do:

The users upload contents via web interface, which will be saved either to the backend MongoDB database or AWS S3 buckets depending on the type of contents users upload. For example, if the user uploads a video, then AWS S3 service will be called, otherwsie MongoDB service will be invoked to store the content.
    

I will also go through the deployments of other sample web applications written in Go and .NET Core detailing all the steps from dockerizing to deployment on K8s.

//NODE.JS Code snippet uploading video file to AWS S3
const AWS = require('aws-sdk');
var fs =  require('fs');
var videoBucket = 'MERNvideoStorage';
var videoKey = 'WeekendCoffee.mp4';
const s3 = new AWS.S3({
    accessKeyID: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
fs.readFile(videoKey, function (err, data) {
  if (err) { throw err; }
     params = {Bucket: videoBucket, Key: videoKey, Body: data };
     s3.putObject(params, function(err, data) {
         if (err) {
             console.log(err)
         } else {
             console.log("Uploaded a video file to MERNvideoStorage bucket in S3");
         }
      });
});


//NODE.JS Code snippet uploading image files to MongoDB using GridFS streaming API
var mongoClient = require('mongodb').MongoClient;
var fs = require('fs');
var url = 'mongodb://mysandpitserver:27017/mernapp';
var imageFile = 'weekendCoffee.png';
mongoClient.connect(url, function(err, db) {
    if (err) {
        console.log('Connection error to MongoDB:', err);
    } else {
        var gfsBucket = new mongodb.GridFSBucket(db, {
            chunkSizeBytes: 1024,
            bucketName: 'mernappdox'
        });
        fs.createReadStream('imageFile').pipe(
            gfsBucket.openUploadStream('imageFile')).on('err', function(err) {
            console.log('GridFS Error:-', err);
        }).on('finish', function() {
            console.log('Document Inserted. Nice one!');
            process.exit(0);
        });
    }
});

Please stay tuned...