{"id":329,"date":"2022-12-22T14:52:56","date_gmt":"2022-12-22T14:52:56","guid":{"rendered":"https:\/\/santosh-shah.com\/blog\/?p=329"},"modified":"2023-02-02T06:08:06","modified_gmt":"2023-02-02T06:08:06","slug":"how-to-upload-images-to-s3-bucket-using-nodejs","status":"publish","type":"post","link":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/","title":{"rendered":"How to upload images to S3 bucket using Nodejs code."},"content":{"rendered":"\n<p>Uploading images to S3 bucket using Nodejs code.  The below code will upload any assets to mentioned AWS S3 bucket and return the uploaded details along with assets size in bytes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const AWS = require('aws-sdk');\nconst { Storage } = require('@google-cloud\/storage');\nconst dotenv = require(\"dotenv\");\ndotenv.config();\n\nconst s3 = new AWS.S3({\n    accessKeyId: process.env.AWS_ACCESS_ID,\n    secretAccessKey: process.env.AWS_SKEY,\n});\n\n\nfunction sizeOf(key, bucket) {\n    return s3.headObject({ Key: key, Bucket: bucket })\n        .promise()\n        .then(res => res.ContentLength);\n}\n\nasync function uploadFileToAws(file){\n    const fileName = `${new Date().getTime()}_${file.name}`;\n    const mimetype = file.mimetype;\n    const params = {\n            Bucket: process.env.AWS_S3_BUCKET,\n            Key: \"s3uploader\/\"+fileName,\n            Body: file.data,\n            ContentType: mimetype,\n            ACL: 'public-read'\n        };\n        const res = await new Promise((resolve, reject) => {\n            s3.upload(params, (err, data) => err == null ? resolve(data) : reject(err));\n          });\n        let meta = await sizeOf(\"s3uploader\/\"+fileName, process.env.AWS_S3_BUCKET)\n        const response = {}\n        response.statusCode = 200;\n        response.body = res;\n        response.body&#91;'size'] = meta;\n        \n        response.headers = {\n            'Access-Control-Allow-Origin': 'http:\/\/localhost:4200',\n            'Access-Control-Allow-Credentials': true,\n        }\n        return response;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Uploading images to S3 bucket using Nodejs code. The below code will upload any assets to mentioned AWS S3 bucket and return the uploaded details along with assets size in bytes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,2],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-aws","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to upload images to S3 bucket using Nodejs code.<\/title>\n<meta name=\"description\" content=\"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to upload images to S3 bucket using Nodejs code.\" \/>\n<meta property=\"og:description\" content=\"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/\" \/>\n<meta property=\"og:site_name\" content=\"Santosh Kumar Shah\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-22T14:52:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-02T06:08:06+00:00\" \/>\n<meta name=\"author\" content=\"Santosh Kumar Shah\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Santosh Kumar Shah\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/\",\"url\":\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/\",\"name\":\"How to upload images to S3 bucket using Nodejs code.\",\"isPartOf\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\"},\"datePublished\":\"2022-12-22T14:52:56+00:00\",\"dateModified\":\"2023-02-02T06:08:06+00:00\",\"author\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\"},\"description\":\"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.\",\"breadcrumb\":{\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/santosh-shah.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to upload images to S3 bucket using Nodejs code.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#website\",\"url\":\"https:\/\/santosh-shah.com\/blog\/\",\"name\":\"Santosh Kumar Shah\",\"description\":\"JavaScript Developer\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f\",\"name\":\"Santosh Kumar Shah\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g\",\"caption\":\"Santosh Kumar Shah\"},\"description\":\"I am JavaScript developer.\",\"sameAs\":[\"https:\/\/santosh-shah.com\/blog\"],\"url\":\"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to upload images to S3 bucket using Nodejs code.","description":"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/","og_locale":"en_US","og_type":"article","og_title":"How to upload images to S3 bucket using Nodejs code.","og_description":"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.","og_url":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/","og_site_name":"Santosh Kumar Shah","article_published_time":"2022-12-22T14:52:56+00:00","article_modified_time":"2023-02-02T06:08:06+00:00","author":"Santosh Kumar Shah","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Santosh Kumar Shah","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/","url":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/","name":"How to upload images to S3 bucket using Nodejs code.","isPartOf":{"@id":"https:\/\/santosh-shah.com\/blog\/#website"},"datePublished":"2022-12-22T14:52:56+00:00","dateModified":"2023-02-02T06:08:06+00:00","author":{"@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f"},"description":"Uploading assets to s3 bucket using nodejs code. Upload videos, images and return absolute url including size of the assets.","breadcrumb":{"@id":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/santosh-shah.com\/blog\/how-to-upload-images-to-s3-bucket-using-nodejs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/santosh-shah.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to upload images to S3 bucket using Nodejs code."}]},{"@type":"WebSite","@id":"https:\/\/santosh-shah.com\/blog\/#website","url":"https:\/\/santosh-shah.com\/blog\/","name":"Santosh Kumar Shah","description":"JavaScript Developer","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/santosh-shah.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/b17cb45bdd5f518e74a08ad2c6c4b39f","name":"Santosh Kumar Shah","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/santosh-shah.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf46c57219d897547f3204b6b302169b3302b17507ccc902946b622a78d0b98b?s=96&d=mm&r=g","caption":"Santosh Kumar Shah"},"description":"I am JavaScript developer.","sameAs":["https:\/\/santosh-shah.com\/blog"],"url":"https:\/\/santosh-shah.com\/blog\/author\/sks7yu\/"}]}},"_links":{"self":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":2,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/santosh-shah.com\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}