{"id":40,"date":"2022-03-09T16:10:30","date_gmt":"2022-03-09T15:10:30","guid":{"rendered":"https:\/\/b.pabst.dev\/?p=40"},"modified":"2022-03-09T16:14:28","modified_gmt":"2022-03-09T15:14:28","slug":"setting-up-nodejs-with-contabo-object-storage","status":"publish","type":"post","link":"https:\/\/b.pabst.dev\/index.php\/2022\/03\/09\/setting-up-nodejs-with-contabo-object-storage\/","title":{"rendered":"Setting up NodeJS with Contabo Object Storage"},"content":{"rendered":"\n<p>Contabo just launched their Object-Storage (<a href=\"https:\/\/contabo.com\/en\/object-storage\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/contabo.com\/en\/object-storage\/<\/a>) today, providing a very solid pricing for a S3-like storage-system.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"560\" height=\"294\" src=\"https:\/\/i0.wp.com\/b.pabst.dev\/wp-content\/uploads\/2022\/03\/image.png?resize=560%2C294&#038;ssl=1\" alt=\"\" class=\"wp-image-42\" srcset=\"https:\/\/i0.wp.com\/b.pabst.dev\/wp-content\/uploads\/2022\/03\/image.png?w=969&amp;ssl=1 969w, https:\/\/i0.wp.com\/b.pabst.dev\/wp-content\/uploads\/2022\/03\/image.png?resize=300%2C158&amp;ssl=1 300w, https:\/\/i0.wp.com\/b.pabst.dev\/wp-content\/uploads\/2022\/03\/image.png?resize=768%2C403&amp;ssl=1 768w, https:\/\/i0.wp.com\/b.pabst.dev\/wp-content\/uploads\/2022\/03\/image.png?resize=600%2C315&amp;ssl=1 600w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" data-recalc-dims=\"1\" \/><figcaption>Pricing taken from contabo.com<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Setup with NodeJS<\/h2>\n\n\n\n<p>To use the Contabo-Storage with NodeJS you can use the official aws-sdk (<a href=\"https:\/\/www.npmjs.com\/package\/aws-sdk\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.npmjs.com\/package\/aws-sdk<\/a>) for development.<\/p>\n\n\n\n<p>To authenticate against the contabo object storage you need configure some things to work:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>\/\/ Import aws-sdk\nconst AWS = require(&quot;aws-sdk&quot;);\n\/\/ Enter copied access KEY and secret key here\nconst ID = &quot;&lt;ACCESS-KEY&gt;&quot;;\nconst SECRET = &quot;&lt;SECRET-KEY&gt;&quot;;<\/code><\/pre><\/div>\n\n\n\n<p>The access and secret key can be taken from the Contabo Object Storage Web-Panel (Account &gt; Security &amp; Access &gt; S3 Object Storage Credentials)<\/p>\n\n\n\n<p>Now let&#8217;s access S3:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>const s3 = new AWS.S3({\n  accessKeyId: ID,\n  secretAccessKey: SECRET,\n  region: &quot;&quot;,\n  endpoint: &quot;https:\/\/eu2.contabostorage.com&quot;,\n  s3BucketEndpoint: true,\n  s3ForcePathStyle: true,\n});<\/code><\/pre><\/div>\n\n\n\n<p>The endpoint can be seen in the Bucket URL in the Contabo Object Storage Web-Panel (Storage &gt; Object Storage &gt; Bucket URL). You should not add the bucket name in the endpoint value though (only the base URL).<\/p>\n\n\n\n<p>Then you only need to define the Bucket to access and can perform operations as usual with aws-S3:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>\/\/ The name of the bucket that you want to access\nconst BUCKET_NAME = &quot;test&quot;;\n\n\/\/ Example of uploading a file\nconst uploadFile = (fileName) =&gt; {\n  \/\/ Read content from the file\n  const fileContent = fs.readFileSync(fileName);\n\n  \/\/ Setting up S3 upload parameters\n  const params = {\n    Bucket: BUCKET_NAME,\n    Key: fileName, \/\/ File name you want to save as in S3\n    Body: fileContent,\n  };\n\n  \/\/ Uploading files to the bucket\n  s3.upload(params, function (err, data) {\n    if (err) {\n      throw err;\n    }\n    console.log(`File uploaded successfully. ${data.Location}`);\n  });\n};\n\n\/\/ Upload .\/test.txt to the bucket\nuploadFile(&quot;test.txt&quot;);<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contabo just launched their Object-Storage (https:\/\/contabo.com\/en\/object-storage\/) today, providing a very solid pricing for a S3-like storage-system. Setup with NodeJS To use the Contabo-Storage with NodeJS you can use the official aws-sdk (https:\/\/www.npmjs.com\/package\/aws-sdk) for development. To authenticate against the contabo object storage you need configure some things to work: The access and secret key can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[13,10,12,9,11],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-useful","tag-api","tag-aws","tag-contabo","tag-nodejs","tag-s3","missing-thumbnail"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":4,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/b.pabst.dev\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}