Skip to content Skip to sidebar Skip to footer

how to do file uploads node js reddit

Upload Files To A Minio Object Storage Cloud With Node.js And Multer

The recent Amazon S3 outage that took down much of the internet inspired me to talk about alternatives. Not as well long ago I wrote about an open source object storage software called Minio and how I was using it on my Raspberry Pi for backups. The great thing about Minio is it shares the aforementioned APIs as AWS S3, but can be deployed to your own hardware, eliminating Amazon equally a dependency.

This time effectually I thought it would be nifty to share how to use Minio equally an object storage for a Node.js application that uses the middleware, Multer, for handling file uploads.

If yous've been keeping up with the web log, you lot'll call up that this isn't the first time I wrote about file uploads in a Node.js application. Not also long ago I wrote well-nigh uploading files via Athwart to a Node.js server with Multer. The instance nosotros explore here won't have a front-end, only information technology shouldn't cease you from creating your own.

The example we build volition use the Minio playground, and so be cautious what you choose to upload. Feel free to use your own Minio case instead.

Create a new Node.js project by executing the following commands:

                                          npm init --y                                                            npm install express minio body-parser multer --save                                                    

The in a higher place commands will create a new package.json file and install all our project dependencies. This volition exist an API based awarding so nosotros'll exist using express and the trunk-parser package for handling endpoints and requests. The multer parcel will handle our file uploads and the minio parcel will allow interactions with a Minio server.

Create a file called app.js which volition concord all our application logic. Open this file and include the following code:

                                          var Express = require("express");                                                            var Multer = crave("multer");                                                            var Minio = require("minio");                                                            var BodyParser = require("body-parser");                                                            var app = Express();                                                                                                                        app.use(BodyParser.json({limit: "4mb"}));                                                                                                                        var minioClient = new Minio.Client({                                                                              endPoint: 'play.minio.io',                                                                              port: 9000,                                                                              secure: true,                                                                              accessKey: 'Q3AM3UQ867SPQQA43P2F',                                                                              secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'                                                            });                                                                                                                        app.post("/upload", Multer({storage: Multer.memoryStorage()}).single("upload"), part(request, response) {                                                                              minioClient.putObject("test", request.file.originalname, request.file.buffer, part(error, etag) {                                                                              if(error) {                                                                              return console.log(error);                                                                              }                                                                              response.transport(request.file);                                                                              });                                                            });                                                                                                                        app.post("/uploadfile", Multer({dest: "./uploads/"}).single("upload"), office(asking, response) {                                                                              minioClient.fPutObject("test", asking.file.originalname, request.file.path, "application/octet-stream", function(error, etag) {                                                                              if(error) {                                                                              return console.log(error);                                                                              }                                                                              response.send(request.file);                                                                              });                                                            });                                                                                                                        app.get("/download", role(request, response) {                                                                              minioClient.getObject("test", request.query.filename, part(mistake, stream) {                                                                              if(error) {                                                                              render response.status(500).transport(error);                                                                              }                                                                              stream.pipe(response);                                                                              });                                                            });                                                                                                                        minioClient.bucketExists("test", function(error) {                                                                              if(error) {                                                                              return console.log(error);                                                                              }                                                                              var server = app.listen(3000, role() {                                                                              console.log("Listening on port %s...", server.accost().port);                                                                              });                                                            });                                                    

In that location are several dissimilar API endpoints in the higher up code with a lot going on. Nosotros're going to interruption it downward and effigy out what everything means.

The start affair we're doing is importing all of the packages that were previously downloaded:

                                          var Express = require("express");                                                            var Multer = require("multer");                                                            var Minio = require("minio");                                                            var BodyParser = require("torso-parser");                                                            var app = Express();                                                    

We are besides initializing Express in the to a higher place lawmaking.

By default, uploads are limited to around 100kb in size. Feel gratuitous to go out it as the default, or ascertain your ain upload limit. For this application, I've set up the upload limit to 4mb.

                                          var minioClient = new Minio.Client({                                                                              endPoint: 'play.minio.io',                                                                              port: 9000,                                                                              secure: true,                                                                              accessKey: 'Q3AM3UQ867SPQQA43P2F',                                                                              secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'                                                            });                                                    

The to a higher place code initializes the Minio SDK for Node.js. We define the server node we wish to connect to and provide the relevant credentials to make it possible. Equally a reminder, in this case we're using the Minio playground where as in production you should utilize your own cloud.

Before we worry virtually the API endpoints, nosotros need to showtime the Node.js server:

                                          minioClient.bucketExists("test", function(mistake) {                                                                              if(error) {                                                                              return console.log(error);                                                                              }                                                                              var server = app.mind(3000, function() {                                                                              console.log("Listening on port %s...", server.accost().port);                                                                              });                                                            });                                                    

Before starting the server we brand sure that the Saucepan exists. At that place are commands to create Buckets via Node.js, merely for this case nosotros assume information technology had already been created.

When it comes to uploading files to a Bucket, there are two options. Nosotros can upload via a stream, or upload as a file.

                                          app.post("/upload", Multer({storage: Multer.memoryStorage()}).single("upload"), function(request, response) {                                                                              minioClient.putObject("examination", asking.file.originalname, asking.file.buffer, function(fault, etag) {                                                                              if(mistake) {                                                                              return console.log(fault);                                                                              }                                                                              response.send(request.file);                                                                              });                                                            });                                                    

In the in a higher place case we are uploading via a stream. This means that the files volition non touch our Node.js filesystem, merely instead go straight into our Saucepan. If using streams, information technology is important to understand the limitations of your server. While more convenient, information technology will be more costly on your server resource.

Y'all also have the option to upload a file that first hits your Node.js filesystem:

                                          app.mail service("/uploadfile", Multer({dest: "./uploads/"}).single("upload"), function(request, response) {                                                                              minioClient.fPutObject("test", request.file.originalname, request.file.path, "awarding/octet-stream", function(error, etag) {                                                                              if(error) {                                                                              return console.log(error);                                                                              }                                                                              response.send(request.file);                                                                              });                                                            });                                                    

In the to a higher place instance, the file will beginning be uploaded to the uploads directory of your projection and then saved to the Minio storage. Just call up to clean the files after they've been uploaded to save space on your Node.js server.

Finally we take an endpoint for downloading information from Minio:

                                          app.get("/download", function(request, response) {                                                                              minioClient.getObject("test", request.query.filename, function(mistake, stream) {                                                                              if(error) {                                                                              render response.status(500).ship(error);                                                                              }                                                                              stream.pipage(response);                                                                              });                                                            });                                                    

In the higher up example, nosotros download an object stream and pipage it straight to the client that requested it. This prevents us from commencement having to download information technology to the Node.js server. However, we have the option to download it first.

Requite the awarding a spin. You tin leverage the power of S3, but in an open source alternative solution.

Conclusion

Yous just saw how to use Node.js and Multer to upload files to a Minio object storage server and download them as well. This is a great alternative to Amazon S3 because information technology is open source and can be deployed to your own servers. The Minio SDK uses the same APIs that you'd expect in AWS S3.

Want to develop a overnice forepart-cease for this Node.js application? Check out the Athwart tutorial I wrote for uploading files to Node.js with Angular.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of mod web and mobile evolution technologies. He has feel in Java, JavaScript, Golang and a variety of frameworks such equally Angular, NativeScript, and Apache Cordova. Nic writes about his evolution experiences related to making spider web and mobile development easier to understand.

stewartsaidee1971.blogspot.com

Source: https://www.thepolyglotdeveloper.com/2017/03/upload-files-minio-object-storage-cloud-node-js-multer/

Post a Comment for "how to do file uploads node js reddit"