How to upload an image to S3 using Node.js

In this post I want to share how to upload an image to AWS S3, the wonderful cloud file hosting solution provided by Amazon Web Services.

I already wrote about this topic in the past in how to upload files to S3 from Node.js

First, install the aws-sdk library:

npm install aws-sdk

Import it in your code at the top of the file you’re going to add this file upload to S3 functionality:

import AWS from 'aws-sdk'

Next, use the SDK to create an instance of the S3 object. I assign it to a s3 variable:

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

Note that I use two environment variables here: AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY.

Now comes some “administrative work”. You need to create an IAM profile on AWS (the credentials) with programmatic access with the permissions for AWSCloudFormationFullAccess and AmazonS3FullAccess and an S3 bucket that this user has access to.

I won’t cover this aspect here as you can find tons of articles and documentation about this. I’ll just talk about the JavaScript code you need.

Now, you need an image blob to upload.

You can use a URL like this:

const imageURL = 'https://url-to-image.jpg'
const res = await fetch(imageURL)
const blob = await res.buffer()

or you can get an image sent from a form image field upload in a multipart form:

const imagePath = req.files[0].path
const blob = fs.readFileSync(imagePath)

Finally, make a call to s3.upload() and call its .promise() method so you can use await to wait until it finishes to get the uploaded file object:

const uploadedImage = await s3.upload({
  Bucket: process.env.AWS_S3_BUCKET_NAME,
  Key: req.files[0].originalFilename,
  Body: blob,
}).promise()

AWS_S3_BUCKET_NAME is the name of the S3 bucket, another environment variable

Finally, you can get the URL of the uploaded image on S3 by referencing the Location property:

uploadedImage.Location

You have to make sure you set the S3 bucket as public so you can access that image URL.


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

In this post I want to share how to upload an image to AWS S3, the wonderful cloud file hosting solution provided by Amazon Web Services.

I already wrote about this topic in the past in how to upload files to S3 from Node.js

First, install the aws-sdk library:

npm install aws-sdk

Import it in your code at the top of the file you’re going to add this file upload to S3 functionality:

import AWS from 'aws-sdk'

Next, use the SDK to create an instance of the S3 object. I assign it to a s3 variable:

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

Note that I use two environment variables here: AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY.

Now comes some “administrative work”. You need to create an IAM profile on AWS (the credentials) with programmatic access with the permissions for AWSCloudFormationFullAccess and AmazonS3FullAccess and an S3 bucket that this user has access to.

I won’t cover this aspect here as you can find tons of articles and documentation about this. I’ll just talk about the JavaScript code you need.

Now, you need an image blob to upload.

You can use a URL like this:

const imageURL = 'https://url-to-image.jpg'
const res = await fetch(imageURL)
const blob = await res.buffer()

or you can get an image sent from a form image field upload in a multipart form:

const imagePath = req.files[0].path
const blob = fs.readFileSync(imagePath)

Finally, make a call to s3.upload() and call its .promise() method so you can use await to wait until it finishes to get the uploaded file object:

const uploadedImage = await s3.upload({
  Bucket: process.env.AWS_S3_BUCKET_NAME,
  Key: req.files[0].originalFilename,
  Body: blob,
}).promise()

AWS_S3_BUCKET_NAME is the name of the S3 bucket, another environment variable

Finally, you can get the URL of the uploaded image on S3 by referencing the Location property:

uploadedImage.Location

You have to make sure you set the S3 bucket as public so you can access that image URL.


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


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2022-04-12T05:00:00+00:00) How to upload an image to S3 using Node.js. Retrieved from https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/

MLA
" » How to upload an image to S3 using Node.js." flaviocopes.com | Sciencx - Tuesday April 12, 2022, https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/
HARVARD
flaviocopes.com | Sciencx Tuesday April 12, 2022 » How to upload an image to S3 using Node.js., viewed ,<https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/>
VANCOUVER
flaviocopes.com | Sciencx - » How to upload an image to S3 using Node.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/
CHICAGO
" » How to upload an image to S3 using Node.js." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/
IEEE
" » How to upload an image to S3 using Node.js." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-node-js/. [Accessed: ]
rf:citation
» How to upload an image to S3 using Node.js | flaviocopes.com | Sciencx | https://www.scien.cx/2022/04/12/how-to-upload-an-image-to-s3-using-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.