{"id":2932,"date":"2019-10-02T16:25:26","date_gmt":"2019-10-02T21:25:26","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2932"},"modified":"2019-10-02T16:26:32","modified_gmt":"2019-10-02T21:26:32","slug":"aws-how-to-write-json-files-to-an-s3-bucket-from-lambda","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2019\/10\/02\/aws-how-to-write-json-files-to-an-s3-bucket-from-lambda\/","title":{"rendered":"AWS: How to write JSON files to an S3 bucket from Lambda"},"content":{"rendered":"\n<p>Let&#8217;s say you&#8217;re working on an API that will create JSON data and you want to store that data in an S3 bucket for retrieval by a separate Lambda script. <\/p>\n\n\n\n<p>Note: For this code example, I am using node.js as my runtime language in my AWS Lambda. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"78\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2019\/10\/image.png\" alt=\"\" class=\"wp-image-2933\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2019\/10\/image.png 363w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2019\/10\/image-300x64.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s say the JSON data has been created and now it is time to write that data to the S3 bucket. <\/p>\n\n\n\n<p>The first step is to set up the following in the <code>require<\/code> portion of your node.js code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const AWS = require('aws-sdk');\nconst S3 = new AWS.S3({region: process.env.AWS_REGION, apiVersion: '2012-10-17'});<\/pre>\n\n\n\n<p>Then add this function to the Lambda:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function putObjectToS3(data, nameOfFile) {\n  \/\/Write to file\n  var s3 = new AWS.S3();\n  var params = {\n      Bucket : \"name_of_s3_bucket\",\n      Key : nameOfFile + \".json\",\n      Body : data\n  }\n  s3.putObject(params, function(err, data) {\n    if (err) console.log(err, err.stack); \/\/ an error occurred\n    else     console.log(\"Put to s3 should have worked: \" + data);           \/\/ successful response\n  });\n}<\/pre>\n\n\n\n<p>Of course before this is called, you will need to set up the S3 bucket with proper read\/write permissions. Then it can be called like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ stringify the consumable data that was created\nvar jsonOutput = JSON.stringify(consumableData);\n\/\/ output the data to confirm it is correct\nconsole.log(jsonOutput);\n\n\/\/ call the function to write the data to the S3 bucket\nputObjectToS3(jsonOutput, \"myData\")<\/pre>\n\n\n\n<p>Then when you inspect the S3 bucket, you should see the file there:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"184\" height=\"36\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2019\/10\/image-1.png\" alt=\"\" class=\"wp-image-2938\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you&#8217;re working on an API that will create JSON data and you want to store that data in an S3 bucket for retrieval by a separate Lambda script. Note: For this code example, I am using node.js as my runtime language in my AWS Lambda. Let&#8217;s say the JSON data has been created &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2019\/10\/02\/aws-how-to-write-json-files-to-an-s3-bucket-from-lambda\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">AWS: How to write JSON files to an S3 bucket from Lambda<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[213],"tags":[210,62,211,58,212],"class_list":["post-2932","post","type-post","status-publish","format-standard","hentry","category-aws","tag-aws","tag-json","tag-lambda","tag-node-js","tag-s3"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2932"}],"version-history":[{"count":6,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2932\/revisions"}],"predecessor-version":[{"id":2940,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2932\/revisions\/2940"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}