How to upload files to S3 from Node.js

I had a simple use case. I was building DEVJOBS, the job board for developers, and it was time to build the form to submit a new job offer.

The recruiter can enter the company details, the job details, and the company logo image.

The data is stored in the database, and at first I tried to store the logo in the database, but after a while I realized that while technically ok, I had some issues with storing the binary data and it was taking too long for the task. So I said, “ok let’s just upload it to S3”.

S3 is one of the wonderful services provided by AWS. Since I already use AWS for other things, adding a S3 bucket is easy.

So I went to create an S3 bucket.

I already had an AWS account. If you don’t, start here: https://aws.amazon.com.

Once you have an account set up, create a IAM user in AWS. Login to AWS, click your name on top and then “My Security Credentials”

On the sidebar click “Users”, and “Add user”. Enable “Programmatic access”.

Move to the next screen via the buttons you find in the bottom of the page (“Next: Permissions”).

Click the “Attach existing policies directly”:

Type “S3” in the filter to show the S3 policies

Select the AmazonS3FullAccess permission:

Once the user is created, you’ll have a pair of access key ID and secret access key. Copy those to your .env file in the project you have, or store them somewhere so you can use them later.

Next, go in S3 and create a bucket. From the S3 homepage https://s3.console.aws.amazon.com click the “Create bucket” button.

Set a name, choose an AWS region, disable “Block all public access” (we’ll get to permissions in another post) and click the Create bucket button at the bottom of the page.

Done! Now it’s time to dive into Node.js. I use this code in Next.js, server side, in an API call.

First install the aws-sdk package with npm install aws-sdk.

As mentioned, store your AWS access codes in .env:

AWS_ACCESS_KEY_ID=<the access key>
AWS_SECRET_ACCESS_KEY=<the secret>
AWS_BUCKET_NAME=<the bucket name>

Add

import AWS from 'aws-sdk'

on top.

Then initialize the s3 object with:

const s3 = new AWS.S3({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})

Now when you want to upload a file, load it from the storage

const filename = 'the-file-name'
const fileContent = fs.readFileSync(fileName)

const params = {
  Bucket: process.env.AWS_BUCKET_NAME,
  Key: `${filename}.jpg`,
  Body: fileContent
}

s3.upload(params, (err, data) => {
  if (err) {
    reject(err)
  }
  resolve(data.Location)
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I had a simple use case. I was building DEVJOBS, the job board for developers, and it was time to build the form to submit a new job offer.

The recruiter can enter the company details, the job details, and the company logo image.

The data is stored in the database, and at first I tried to store the logo in the database, but after a while I realized that while technically ok, I had some issues with storing the binary data and it was taking too long for the task. So I said, “ok let’s just upload it to S3”.

S3 is one of the wonderful services provided by AWS. Since I already use AWS for other things, adding a S3 bucket is easy.

So I went to create an S3 bucket.

I already had an AWS account. If you don’t, start here: https://aws.amazon.com.

Once you have an account set up, create a IAM user in AWS. Login to AWS, click your name on top and then “My Security Credentials”

On the sidebar click “Users”, and “Add user”. Enable “Programmatic access”.

Move to the next screen via the buttons you find in the bottom of the page (“Next: Permissions”).

Click the “Attach existing policies directly”:

Type “S3” in the filter to show the S3 policies

Select the AmazonS3FullAccess permission:

Once the user is created, you’ll have a pair of access key ID and secret access key. Copy those to your .env file in the project you have, or store them somewhere so you can use them later.

Next, go in S3 and create a bucket. From the S3 homepage https://s3.console.aws.amazon.com click the “Create bucket” button.

Set a name, choose an AWS region, disable “Block all public access” (we’ll get to permissions in another post) and click the Create bucket button at the bottom of the page.

Done! Now it’s time to dive into Node.js. I use this code in Next.js, server side, in an API call.

First install the aws-sdk package with npm install aws-sdk.

As mentioned, store your AWS access codes in .env:

AWS_ACCESS_KEY_ID=<the access key>
AWS_SECRET_ACCESS_KEY=<the secret>
AWS_BUCKET_NAME=<the bucket name>

Add

import AWS from 'aws-sdk'

on top.

Then initialize the s3 object with:

const s3 = new AWS.S3({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})

Now when you want to upload a file, load it from the storage

const filename = 'the-file-name'
const fileContent = fs.readFileSync(fileName)

const params = {
  Bucket: process.env.AWS_BUCKET_NAME,
  Key: `${filename}.jpg`,
  Body: fileContent
}

s3.upload(params, (err, data) => {
  if (err) {
    reject(err)
  }
  resolve(data.Location)
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-22T05:00:00+00:00) How to upload files to S3 from Node.js. Retrieved from https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/

MLA
" » How to upload files to S3 from Node.js." flaviocopes.com | Sciencx - Tuesday June 22, 2021, https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/
HARVARD
flaviocopes.com | Sciencx Tuesday June 22, 2021 » How to upload files to S3 from Node.js., viewed ,<https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/>
VANCOUVER
flaviocopes.com | Sciencx - » How to upload files to S3 from Node.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/
CHICAGO
" » How to upload files to S3 from Node.js." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/
IEEE
" » How to upload files to S3 from Node.js." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/. [Accessed: ]
rf:citation
» How to upload files to S3 from Node.js | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/22/how-to-upload-files-to-s3-from-node-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.