I had a unproblematic use case. I was building a job board and information technology was fourth dimension to build the course to submit a new task offer.

The recruiter can enter the visitor 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, only 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'due south but upload it to S3".

S3 is one of the wonderful services provided by AWS. Since I already apply AWS for other things, adding a S3 bucket is like shooting fish in a barrel.

So I went to create an S3 bucket.

I already had an AWS account. If y'all 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 together user". Enable "Programmatic access".

Move to the next screen via the buttons y'all detect in the bottom of the page ("Next: Permissions").

Click the "Attach existing policies straight":

Type "S3" in the filter to bear witness the S3 policies

Select the AmazonS3FullAccess permission:

Once the user is created, yous'll have a pair of admission key ID and hole-and-corner access key. Copy those to your .env file in the project you accept, or store them somewhere so you can use them later.

Next, become in S3 and create a saucepan. From the S3 homepage https://s3.console.aws.amazon.com click the "Create bucket" button.

Prepare a proper 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'southward time to dive into Node.js. I use this lawmaking in Adjacent.js, server side, in an API call.

Start 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 underground> AWS_BUCKET_NAME=<the bucket proper name>        

Add together

                          import              AWS              from              'aws-sdk'                      

on top.

So initialize the s3 object with:

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

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

                          const              filename              =              'the-file-name'              const              fileContent              =              fs.readFileSync(fileName)              const              params              =              {              Saucepan              :              process.env.AWS_BUCKET_NAME,              Central              :              `              ${              filename              }              .jpg`,              Body              :              fileContent              }              s3.upload(params, (err,              data) => {              if              (err) {              reject(err)   }              resolve(data.Location) })