{"id":2226,"date":"2025-09-08T10:32:20","date_gmt":"2025-09-08T10:32:20","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2226"},"modified":"2026-02-05T11:59:22","modified_gmt":"2026-02-05T11:59:22","slug":"how-to-upload-an-image-to-aws-s3-using-node-js","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-to-upload-an-image-to-aws-s3-using-node-js\/","title":{"rendered":"How to Upload an Image to AWS S3 Using Node.js?"},"content":{"rendered":"\n<p>Uploading files, especially images, to Amazon S3 is a common requirement in modern web applications. AWS S3 (Simple Storage Service) is highly scalable, durable, and secure \u2014 making it ideal for storing images, documents, and other assets.<\/p>\n\n\n\n<p>This blog will show you how to upload an image to S3 using Node.js, step by step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Upload an Image to AWS S3 Using Node.js?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Set Up Your AWS Credentials<\/h3>\n\n\n\n<p><strong>First, make sure you have:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An <a href=\"https:\/\/aws.amazon.com\/\" data-type=\"link\" data-id=\"https:\/\/aws.amazon.com\/\" target=\"_blank\" rel=\"noopener\">AWS account<\/a><\/li>\n\n\n\n<li>Access Key ID &amp; Secret Access Key<\/li>\n\n\n\n<li>An S3 bucket created (e.g., my-image-bucket)<\/li>\n<\/ul>\n\n\n\n<p><strong>Install AWS CLI (optional) and configure:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws configure<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Install Required Packages<\/h3>\n\n\n\n<p>You need the AWS SDK and optionally multer if you want to handle uploads via an API.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install aws-sdk multer<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure AWS SDK in Node.js<\/h3>\n\n\n\n<p><strong>Create a file named s3Upload.js:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const AWS = require('aws-sdk');\nconst fs = require('fs');\nconst path = require('path');\n\/\/ Load credentials from env or AWS config\nAWS.config.update({\naccessKeyId: process.env.AWS_ACCESS_KEY_ID,\nsecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,\nregion: 'us-east-1', \/\/ replace with your bucket region\n});\nconst s3 = new AWS.S3();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Upload Image to S3 Bucket<\/h3>\n\n\n\n<p>You can upload an image either from disk or from a buffer. Here&#8217;s an example from the local filesystem:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const uploadImage = async () => {\nconst filePath = path.join(__dirname, 'sample.jpg');\nconst fileContent = fs.readFileSync(filePath);\nconst params = {\nBucket: 'my-image-bucket',\nKey: 'uploads\/sample.jpg', \/\/ folder + file name in bucket\nBody: fileContent,\nContentType: 'image\/jpeg',\nACL: 'public-read', \/\/ optional: makes file publicly accessible\n};\ntry {\nconst data = await s3.upload(params).promise();\nconsole.log('Upload Success:', data.Location);\n} catch (err) {\nconsole.error('Upload Error:', err.message);\n}\n};\nuploadImage();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Uploading images to AWS S3 using Node.js is pretty straightforward once your AWS SDK is set up and your credentials are ready. Whether you\u2019re dealing with files stored locally or handling uploads coming through an API, this method works well and can handle big, real-world apps.<\/p>\n\n\n\n<p>For teams building enterprise applications, it\u2019s common to <a href=\"https:\/\/www.cmarix.com\/hire-nodejs-developers.html\">hire Node.js developers<\/a> who know AWS services inside out. This helps keep uploads running smoothly and keeps your data safe.<\/p>\n\n\n\n<p>In short, with the right setup and expertise, managing image uploads to S3 stays efficient and secure as your app grows.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uploading files, especially images, to Amazon S3 is a common requirement in modern web applications. AWS S3 (Simple Storage Service) is highly scalable, durable, and secure \u2014 making it ideal for storing images, documents, and other assets. This blog will show you how to upload an image to S3 using Node.js, step by step. How [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[18,3],"tags":[],"class_list":["post-2226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=2226"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2226\/revisions"}],"predecessor-version":[{"id":2230,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2226\/revisions\/2230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2227"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}