Upload Image to S3 Bucket Node Js

Uploading files to AWS S3 using Nodejs

Uploading files to AWS S3 using Nodejs

AWS S3. A place where you lot can store files. That's what well-nigh of you lot already know about information technology. S3 is one of the older service provided past Amazon, earlier the days of revolutionary Lambda functions and game irresolute Alexa Skills.You tin can shop almost whatever type of files from md to pdf, and of size ranging from 0B to 5TB.

According to their official docs, AWS S3 is,

"S3 provides comprehensive security and compliance capabilities that run across even the well-nigh stringent regulatory requirements. It gives customers flexibility in the way they manage data for cost optimization, access control, and compliance. " - AWS Docs

If I attempt to put information technology in simple terms, AWS S3, is an object based storage organisation where every file your shop is saved equally object not file. At that place are many big tech-wigs, which uses S3, and Dropbox is one of them. Recently, Dropbox starts saving metadata of their file in their own service but they are withal saving principal data in S3. Why? Well, S3 is not that expensive and it'southward 99.9% available. Plus, y'all go the alter to use services like Glacier which can save data and charge almost $0.01 per GB.

And so far, if I accept gotten your attention, and you're thinking how to use S3 in my nodejs application. Well, you don't have to wait for long.

AWS has official package which exposes S3 apis for node js apps and makes it also easy for developers to admission S3 from their apps.

Source: https://youtu.be/FLolHgKRTKg

In next few steps, I will guide you to build a nodejs based app, which tin write any file to AWS S3.

1. Set up node app

A basic node app usually have 2 file, package.json (for dependencies)  and a starter file (like app.js, index.js, server.js).

You tin can use your Bone's file manager or your favourite IDE to create projection but I usually prefer CLI. And so, let'south go into our shell and follow these commands:

mkdir s3-contacts-upload-demo
cd s3-contacts-upload-demo
affect index.js .gitignore
npm init

If y'all didn't go any error in above commands, you lot will accept a binder past the proper name of s3-contacts-upload-demo, 3 files in it, bundle.json, .gitignore andindex.json. You can employ this file to add together file in your .gitignore file, so that these won't get committed to github or some other version control.

npm init creates a package.json, which take project's details, you can just hit enter in your shell for default values if yous wish.

Set up node app

2. Install dependencies

Let'due south start past installing the NPM package:

npm install --save aws-sdk

Later on successful installation of this bundle, you lot can likewise bank check your parcel.json file, it will take aws-sdk listed in "dependencies" field.

This npm package can be used to admission any AWS service from your nodejs app, and here we will use it for S3.

three. Import packages

Once installed, import the bundle in your lawmaking:

const fs = require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
  accessKeyId: process.env.AWS_ACCESS_KEY,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

Every bit you can notice, nosotros have likewise imported fs parcel which will exist used to write file data in this app. Nosotros are also using environment variables, to prepare upwards AWS access and secret access primal, as it is a bad practice to put them on version control like Github or SVN.

Now, you take your S3 instance, which can admission all the buckets in your AWS account.

four. Pass saucepan information and write business logic

Below is a uncomplicated prototype of how to upload file to S3. Here, Bucket is proper name of your bucket and primal is name of subfolder. So, if your bucket name is "test-bucket" and you want to save file in "exam-saucepan/folder/subfolder/file.csv", so value of Key should exist "older/subfolder/file.csv".

Note: S3 is object based storage and not file based. So, even in AWS console yous tin see nested folders, behind the scene they never go saved similar that. Every object has ii fields: Primal and Value. Primal here is but proper noun of file and Value is data which is getting stored.

So, if a saucepan "bucket1" has key "key1/key2/file.mp3", yous can visualize it like this:

{
"bucket1": {
"key1/key2/file.mp3": "<mp3-data>"
}
}

Below is unproblematic snippe to upload a file,using Primal and BucketName.

const params = {
 Bucket: 'saucepan',
 Key: 'key',
 Trunk: stream
};

s3.upload(params, role(err, data) {
console.log(err, data);
});

v. File to upload to S3

Offset, create a file, permit's say contacts.csv and write some information in information technology.

 File to upload to S3

Higher up is some dummy data for you to get started. Now yous accept a contacts.csv file, let's read information technology using fs module and save it to S3.

S3 upload method returns mistake and data in callback, where data field contains location, bucket and key of uploaded file. For complete API reference, refer their official docs.

Now run this app by following control:

AWS_ACCESS_KEY=<your_access_key>
AWS_SECRET_ACCESS_KEY=<your_secret_key>
node index.js

Nosotros have passed our AWS keys as surroundings variables.

If you don't get any error in above snippet, your bucket should have the file.

And that'due south it, nether xxx lines of code you uploaded a file to AWS S3.

Yous tin can find the full projection here.

At that place are lot many things you can exercise with this package like

  • Listing out buckets and objects

  • Gear up permissions on bucket

  • Create, get or delete bucket and much more than

I would highly recommend y'all to through this medico for APIs, they are very well explained with parameters yous tin pass to each API and response format they return.

I hope you constitute this postal service useful and please do let united states of america know in case of any question or query.

  • Node JS

Useful Links

burnshuserapposse.blogspot.com

Source: https://www.zeolearn.com/magazine/uploading-files-to-aws-s3-using-nodejs

0 Response to "Upload Image to S3 Bucket Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel