How to Upload a File to S3 With Node

Uploading files to AWS S3 using Nodejs

Uploading files to AWS S3 using Nodejs

AWS S3. A place where you tin can store files. That's what nigh of you already know about it. S3 is one of the older service provided past Amazon, before the days of revolutionary Lambda functions and game changing Alexa Skills.You can store nigh whatsoever type of files from doctor to pdf, and of size ranging from 0B to 5TB.

Co-ordinate to their official docs, AWS S3 is,

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

If I try to put it in simple terms, AWS S3, is an object based storage arrangement where every file your store is saved as object non file. There 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 still saving primary data in S3. Why? Well, S3 is not that expensive and it'southward 99.9% available. Plus, you lot become the modify to use services like Glacier which tin salve data and charge almost $0.01 per GB.

Then far, if I have gotten your attention, and you're thinking how to apply S3 in my nodejs application. Well, you don't have to expect for long.

AWS has official package which exposes S3 apis for node js apps and makes information technology too like shooting fish in a barrel for developers to access 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 can write whatsoever file to AWS S3.

1. Gear up node app

A bones node app usually take ii file, package.json (for dependencies)  and a starter file (similar app.js, index.js, server.js).

You tin can use your Os's file managing director or your favourite IDE to create project simply I ordinarily prefer CLI. Then, let'south go into our shell and follow these commands:

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

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

npm init creates a packet.json, which take project's details, you tin just hit enter in your crush for default values if you wish.

Set up node app

2. Install dependencies

Allow's start by installing the NPM package:

npm install --save aws-sdk

Later on successful installation of this package, y'all can also cheque your package.json file, it will take aws-sdk listed in "dependencies" field.

This npm package can exist used to access whatsoever AWS service from your nodejs app, and here nosotros volition utilise it for S3.

iii. Import packages

One time installed, import the package in your lawmaking:

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

As y'all tin notice, we have also imported fs package which will be used to write file information in this app. We are also using environment variables, to set up AWS admission and hush-hush access key, as it is a bad practice to put them on version control like Github or SVN.

Now, you have your S3 instance, which can admission all the buckets in your AWS business relationship.

4. Pass saucepan information and write business logic

Below is a elementary prototype of how to upload file to S3. Here, Bucket is name of your bucket and key is name of subfolder. So, if your bucket proper name is "test-bucket" and yous want to salve file in "test-saucepan/folder/subfolder/file.csv", then value of Central should be "older/subfolder/file.csv".

Note: S3 is object based storage and not file based. Then, even in AWS console you tin see nested folders, behind the scene they never get saved like that. Every object has two fields: Key and Value. Key hither is simply name of file and Value is information which is getting stored.

So, if a bucket "bucket1" has fundamental "key1/key2/file.mp3", y'all can visualize it like this:

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

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

const params = {
 Saucepan: 'saucepan',
 Key: 'key',
 Body: stream
};

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

v. File to upload to S3

Showtime, create a file, let's say contacts.csv and write some data in it.

 File to upload to S3

Above is some dummy data for you to get started. At present you lot take a contacts.csv file, allow's read it using fs module and save it to S3.

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

At present run this app past following control:

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

We have passed our AWS keys equally environs variables.

If you don't become any mistake in in a higher place snippet, your saucepan should have the file.

And that's it, under thirty lines of lawmaking you uploaded a file to AWS S3.

You can find the full project here.

There are lot many things yous can practice with this package similar

  • List out buckets and objects

  • Fix permissions on bucket

  • Create, get or delete bucket and much more than

I would highly recommend you lot to through this doc for APIs, they are very well explained with parameters you tin laissez passer to each API and response format they return.

I promise you lot found this post useful and please do let us know in instance of whatsoever question or query.

  • Node JS

Useful Links

bensonexame1938.blogspot.com

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

0 Response to "How to Upload a File to S3 With Node"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel