Using AWS S3 Buckets in a NodeJS App
AWS S3 is a simple storage service offered by Amazon AWS that give you the ability to use it as a webservice. Imagine a Dropbox or Google Drive that allows you to upload or download files programmatically with an API.
We will go through all the process of using a S3 with NodeJS, from creating the S3 Bucket and access credentials to actually use the bucket.
First, you need to know how a few concepts:
- S3 Bucket: is a storage unit from the S3 service from Amazon. You can store different type of files (data) and have metadata that describes the actual data or file.
- Access Key ID: Is the identification for a certain access key that allows an application or user to access a set of preconfigured AWS Resource (like a S3 Bucket for example).
- Secret Access key: Is the secret part of the Access Key ID. Think of it as a password for a specific Access Key ID (they must be used always in pairs)
Now that we understand all of this concepts, we must configure the Amazon Console before start coding.
Step 1: Get your key pairs
The easy way to obtain a key pair is to create them for your default account for AWS Console.
Go to the top bar and click your user account
Then, click in “My security credentials” and “Access keys”, finally, click “Create New Access Key”.
A modal window should pop and it will tell you that your access key pairs have been created. Click in “Show access key” to see both of the keys
Copy and store them in a secure place. We will use them later.
Step 2: Create a Bucket
Go to “Services/Storage/S3” and then click in “Create Bucket”.
Then, fill the information required in the form. The bucket name must be unique, so be creative :)
For this example we are leaving the properties and permissions with the default values
Click next, next, next and you will have the bucket created.
The way that we will need to identificate the bucket is with the “Bucket name” (in this case is “creative-bucket-name-6355123”). So keep it in mind for later use.
Step 3: Creating a Node JS project an example file for upload
Before you start coding, let’s create the basic project.
Create a blank project using the command and typing the information required (project name, repo, licence, etc).
Then, create a folder in the root directory with the name “data” and create a text file called “file.txt” with the following content:
Hi, I am a file uploaded to S3 resource using NodeJS :)
Now, go to the root folder and write the following command to install the AWS Package of NPM inside the project.
npm install aws-sdk --save
Now, create a “index.js” file in the root folder and put the following content
Now, replace the following text:
- Replace in Line 7, “<Access Key Here>” with the Access Key ID from the step 1.
- Replace in Line 8, “<Secret Access Key Here>” with the Secret Access Keyfrom the step 1.
- Replace in Line 16, “<Bucket Name here>” with your Bucket name created in the step 2.
All set, if you run “npm start” you should see a console message indicating the URL of the file uploaded. Go to your AWS console and check if your file is up.
The key parts of this code are:
- Line 6: Access configuration
- Line 12: File to upload
- Line 15: Upload parameters
- Line 21: Uploading file and response callback (second parameter with the function).
- Line 23: Handling error
That’s it! you can leave questions or doubts that you could have in the comments area bellow and I will complement the post.