Aws Node Sdk S3 Upload New Item
Uploading files to AWS S3 using Nodejs
AWS S3. A place where y'all tin shop files. That'south what most of you already know about it. S3 is 1 of the older service provided by Amazon, before the days of revolutionary Lambda functions and game changing Alexa Skills.You can store virtually any type of files from doctor 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 meet even the near stringent regulatory requirements. It gives customers flexibility in the style they manage data for cost optimization, access control, and compliance. " - AWS Docs
If I try to put information technology in simple terms, AWS S3, is an object based storage system where every file your store is saved as object not 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 simply they are all the same saving primary information in S3. Why? Well, S3 is not that expensive and it's 99.9% bachelor. Plus, you lot become the change to apply services like Glacier which can save data and charge well-nigh $0.01 per GB.
So far, if I accept gotten your attention, and you're thinking how to employ S3 in my nodejs awarding. Well, you don't have to look for long.
AWS has official package which exposes S3 apis for node js apps and makes it also piece of cake for developers to access S3 from their apps.
Source: https://youtu.be/FLolHgKRTKg
In adjacent few steps, I will guide you to build a nodejs based app, which can write whatever file to AWS S3.
1. Fix node app
A basic node app ordinarily have 2 file, parcel.json (for dependencies) and a starter file (similar app.js, index.js, server.js).
You lot can utilize your Bone's file manager or your favourite IDE to create projection just I usually prefer CLI. So, allow's go into our shell and follow these commands:
mkdir s3-contacts-upload-demo
cd s3-contacts-upload-demo
touch on index.js .gitignore
npm init
If you didn't get any error in to a higher place commands, you volition have a folder by the name of s3-contacts-upload-demo, 3 files in information technology, package.json, .gitignore andindex.json. Yous tin can use 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 have projection'southward details, you lot can merely hit enter in your shell for default values if you wish.
two. Install dependencies
Let's kickoff past installing the NPM packet:
npm install --save aws-sdk
After successful installation of this package, you can as well cheque your package.json file, it will have aws-sdk listed in "dependencies" field.
This npm package can be used to access any AWS service from your nodejs app, and hither nosotros will use it for S3.
three. Import packages
One time installed, import the package in your code:
const fs = require('fs');
const AWS = crave('aws-sdk');
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: procedure.env.AWS_SECRET_ACCESS_KEY
});
As you tin can notice, we have also imported fs package which will be used to write file data in this app. Nosotros are too using surround variables, to set AWS access and secret access central, as it is a bad practice to put them on version control like Github or SVN.
At present, you accept your S3 example, which can access all the buckets in your AWS account.
iv. Laissez passer bucket data and write business logic
Beneath is a simple image of how to upload file to S3. Here, Bucket is name of your saucepan and cardinal is name of subfolder. Then, if your saucepan name is "test-bucket" and you desire to relieve file in "test-bucket/folder/subfolder/file.csv
", then value of Key should be "older/subfolder/file.csv
".
Note: S3 is object based storage and not file based. So, even in AWS console you can see nested folders, behind the scene they never go saved like that. Every object has two fields: Key and Value. Fundamental here is only name of file and Value is data which is getting stored.
So, if a bucket "bucket1" has key "key1/key2/file.mp3", you lot can visualize it similar this:
{
"bucket1": {
"key1/key2/file.mp3": "<mp3-data>"
}
}
Below is simple snippe to upload a file,using Fundamental and BucketName.
const params = {
Bucket: 'bucket',
Key: 'key',
Body: stream
};
s3.upload(params, function(err, data) {
console.log(err, data);
});
5. File to upload to S3
First, create a file, let'south say contacts.csv and write some data in it.
Above is some dummy information for you to get started. At present you have a contacts.csv file, let'southward read information technology using fs module and save it to S3.
S3 upload method returns error and data in callback, where information field contains location, bucket and fundamental of uploaded file. For consummate API reference, refer their official docs.
Now run this app past following control:
AWS_ACCESS_KEY=<your_access_key>
AWS_SECRET_ACCESS_KEY=<your_secret_key> node index.js
Nosotros take passed our AWS keys as environs variables.
If you don't get whatever mistake in higher up snippet, your bucket should have the file.
And that's it, under 30 lines of code you lot uploaded a file to AWS S3.
You lot tin can notice the total project here.
There are lot many things you can do with this package similar
-
List out buckets and objects
-
Gear up permissions on bucket
-
Create, get or delete bucket and much more
I would highly recommend you to through this doc for APIs, they are very well explained with parameters you can pass to each API and response format they return.
I hope yous constitute this post useful and please exercise let us know in case of any question or query.
- Node JS
Useful Links
Source: https://www.zeolearn.com/magazine/uploading-files-to-aws-s3-using-nodejs
0 Response to "Aws Node Sdk S3 Upload New Item"
Post a Comment