<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: iamviveksuthar</title>
    <description>The latest articles on Forem by iamviveksuthar (@theviveksuthar).</description>
    <link>https://forem.com/theviveksuthar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F922353%2F962dfe1c-11f1-4e1d-b601-73866517c913.jpeg</url>
      <title>Forem: iamviveksuthar</title>
      <link>https://forem.com/theviveksuthar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/theviveksuthar"/>
    <language>en</language>
    <item>
      <title>Upload Your Files to Filebase from your CLI</title>
      <dc:creator>iamviveksuthar</dc:creator>
      <pubDate>Fri, 07 Apr 2023 14:14:24 +0000</pubDate>
      <link>https://forem.com/theviveksuthar/upload-your-files-to-filebase-from-your-cli-poo</link>
      <guid>https://forem.com/theviveksuthar/upload-your-files-to-filebase-from-your-cli-poo</guid>
      <description>&lt;p&gt;If you are building a decentralized App, in most cases you need to store your images, files, videos on a decentralized platform like IPFS, File coin or Filebase ,so in this blog we will discuss how you can upload your files to File-base.&lt;/p&gt;

&lt;p&gt;Before jumping to code, first lets discuss what is decentralized storage?&lt;/p&gt;

&lt;p&gt;So As per name, Decentralized storage means it is not centralized and all of your data is not stored at some data-center that is controlled by some few. In Decentralized storage,&lt;br&gt;
users can store their data regardless of centers. This system uses peer-to-peer network-model to achieve decentralization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;IPFS[Inter Planetary File System] is a peer-to-peer hypermedia protocol designed to store and share data in a decentralized manner.&lt;a href="https://ipfs.io/"&gt;Know about IPFS&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;File-base is the first S3-compatible object storage platform that allows you to store data.&lt;a href="https://filebase.com/"&gt;More about File-base&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let us  start our journey to uploading files to file-base.&lt;/p&gt;

&lt;p&gt;Here, we are going to upload files using the command-line , in the next blog I will show how you can do this with user-input and with a cool front-end.  &lt;/p&gt;

&lt;p&gt;Prerequisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NodeJS installed on your Machine
[If not you can install from here &lt;a href="https://nodejs.org/en/download/"&gt;Download NodeJS&lt;/a&gt;]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-A File-base Account &lt;a href="https://console.filebase.com/signup"&gt;You can create-one for free&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-A basic knowledge of JavaScript(of course not mandatory)&lt;/p&gt;

&lt;p&gt;So Let's start.&lt;/p&gt;

&lt;p&gt;1) Open your Terminal and run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir filebaseupload-demo
cd filebaseupload-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new folder named **filebaseupload-demo **by cd command we will enter in our folder.&lt;/p&gt;

&lt;p&gt;2) Run following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a package.json&lt;/p&gt;

&lt;p&gt;3) Now open our folder &lt;strong&gt;filebaseupload-demo&lt;/strong&gt; with your code editor(VS-Code, Sublime, Atom or Whatever you use)&lt;/p&gt;

&lt;p&gt;I prefer to use VS-Code.&lt;/p&gt;

&lt;p&gt;At this Point your Folder-structure should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TTOYDYx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o29p8p9i1iw2j0lvazeg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TTOYDYx_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o29p8p9i1iw2j0lvazeg.jpg" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4)Now create a file named &lt;code&gt;index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5)As we are using File-base's S3 compatible API, We need to add some dependencies&lt;br&gt;
Now Open Your Terminal in VS Code and run following commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install aws-sdk dotenv fs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;aws-sdk&lt;/code&gt; : aws-sdk for interacting with aws-services throught Javascript&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dotenv&lt;/code&gt; : loads our environment variable from .env to &lt;code&gt;process.env.VARIABLE_NAME&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs&lt;/code&gt; : packge used to read and write files &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6) Now, create a &lt;strong&gt;.env&lt;/strong&gt; file in our root directory &lt;/p&gt;

&lt;p&gt;Now in .env file add environment variable that we need&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWS_ACCESS_KEY_ID=YOUR_ACCES_KEY_FROM_FILEBASE
AWS_SECRET_ACCESS_KEY=YOUR_ SECRET_KEY_FROM_FILEBASE
BUCKET_NAME=YOUR_FILEBASE_BUCKET_NAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a guide you can get your keys:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;a href="https://console.filebase.com/"&gt;Filebase console&lt;/a&gt; and sign-in&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now log-in with your account &lt;br&gt;
On successful login you should see screen like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n5xHihga--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16ihh0wxl5mghqztakqk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n5xHihga--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16ihh0wxl5mghqztakqk.jpg" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Click on Buckets in left-sidebar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y5AquPa2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihqsb2wq126f1rcgbhw5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y5AquPa2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihqsb2wq126f1rcgbhw5.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now click on create bucket&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VL60hTPR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmilqq5tv66l6o6rvqg4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VL60hTPR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmilqq5tv66l6o6rvqg4.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Enter bucket name &lt;code&gt;demo-upload&lt;/code&gt; and click on &lt;code&gt;create-bucket&lt;/code&gt; button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UvzAUA9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18vflj83fc01l9fxaf8q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UvzAUA9C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18vflj83fc01l9fxaf8q.jpg" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now click on bucket name&lt;/p&gt;

&lt;p&gt;Now click on Access Keys in Sidebar and you should see your keys&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wr8fwydo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo4hmfgfe5evj2mixeu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wr8fwydo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo4hmfgfe5evj2mixeu1.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;copy KEY and paste at YOUR_ACCES_KEY_FROM_FILEBASE in .env&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;copy SECRET and paste at YOUR_ SECRET_KEY_FROM_FILEBASE in .env&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in BUCKET_NAME write our bucket name in our case it is &lt;code&gt;demo-upload&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7) Now Open your  &lt;code&gt;index.js&lt;/code&gt; and paste this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();
const AWS = require("aws-sdk");
const fs = require("fs");
const s3 = new AWS.S3({
    endpoint: 'https://s3.filebase.com',
    region: 'us-east-1',
    signatureVersion: 'v4'
});
fs.readFile('BAYC.png', (err, data) =&amp;gt; {
    if (err) {
        console.log(err);
        return;
    }
    const params = {
        Bucket: process.env.BUCKET_NAME,
        Key: 'test/BAYC.png',
        ContentType: 'image/png',
        Body: data,
    }
    const request = s3.putObject(params);
    request.on('httpHeaders', (statusCode, headers) =&amp;gt; {
        console.log(`CID:${headers['x-amz-meta-cid']}`);
        console.log(`Go to https://ipfs.filebase.io/ipfs/${headers['x-amz-meta-cid']}`);
    });
    request.send();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Lets under-stand our code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();
const AWS = require("aws-sdk");
const fs = require("fs");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, We are importing our aws-sdk and fs module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const s3 = new AWS.S3({
    endpoint: 'https://s3.filebase.com',
    region: 'us-east-1',
    signatureVersion: 'v4'
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new instance of aws's  S3-storage service&lt;br&gt;
with given configuration endpoint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fs.readFile('BAYC.png', (err, data) =&amp;gt; {
    if (err) {
        console.log(err);
        return;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are reading our file in our directory which is &lt;a href="https://ipfs.filebase.io/ipfs/Qma9YuNh8x8V6QxP6VHvma1tAZP5BJqdMvqnyZqJizyJFt"&gt;BAYC Image&lt;/a&gt; to upload on File-base.&lt;br&gt;
We are using fs modules's readfile method to read the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const params = {
        Bucket: process.env.BUCKET_NAME,
        Key: 'test/BAYC.png',
        ContentType: 'image/png',
        Body: data,
    }
    const request = s3.putObject(params);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have read the file.&lt;/p&gt;

&lt;p&gt;We are declaring our parameters of object with Bucket-name, Key[Where to store file in bucket],Content type of image, and Body.&lt;/p&gt;

&lt;p&gt;After that we are calling s3's pubObject method with our parameters&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; request.on('httpHeaders', (statusCode, headers) =&amp;gt; {
        console.log(`CID:${headers['x-amz-meta-cid']}`);
        console.log(`Go to https://ipfs.filebase.io/ipfs/${headers['x-amz-meta-cid']}`);
    });
request.send();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;request.on() is function that listens to events emitted by remote server.&lt;/p&gt;

&lt;p&gt;When our remote server sends the httpHeaders, the listener function is called &lt;br&gt;
and inside that we are logging our CID by using headers['x-amz-meta-cid]&lt;br&gt;
and we are generating link for our file by &lt;br&gt;
&lt;code&gt;https://ipfs.filebase.io/ipfs/${headers['x-amz-meta-cid']}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that In console.log we are using &lt;code&gt;&lt;/code&gt;&lt;code&gt;Backticks&lt;/code&gt; (template literals ) and not regular &lt;br&gt;
&lt;code&gt;""&lt;/code&gt; double quotes.&lt;/p&gt;

&lt;p&gt;Now lets run our file&lt;br&gt;
go to terminal and run &lt;code&gt;node index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If everything goes ok, you should see output  like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7iP3jkrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5s7ifcxz273jr7nu4d8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7iP3jkrb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5s7ifcxz273jr7nu4d8.jpg" alt="Image description" width="800" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you copy the link and paste it in browser you should see the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1d2ts70b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9gxrcj2tety2nz7h4l8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1d2ts70b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9gxrcj2tety2nz7h4l8.jpg" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;br&gt;
Now if you go to your file-base and navigate to your bucket you should see the image is uploaded there&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_qFfpAEd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utbf4qxyij193mi33wcv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_qFfpAEd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utbf4qxyij193mi33wcv.jpg" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see ,take a minute and reward you-self that you followed the entire blog&lt;/p&gt;

&lt;p&gt;Until next&lt;br&gt;
Keep coding ,&lt;br&gt;
Keep debugging&lt;/p&gt;

&lt;p&gt;Feel free to contact me&lt;br&gt;
 &lt;a href="https://github.com/VIVEK-SUTHAR"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RYSbecBR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://img.shields.io/badge/Github-%25231DA1F2.svg%3Flogo%3DGithub%26logoColor%3Dblack" alt="Github" width="65" height="20"&gt;&lt;/a&gt; &lt;a href="https://linkedin.com/in/VIVEK_SUTHAR"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vzAfJio0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://img.shields.io/badge/LinkedIn-%25230077B5.svg%3Flogo%3Dlinkedin%26logoColor%3Dwhite" alt="LinkedIn" width="75" height="20"&gt;&lt;/a&gt; &lt;a href="https://twitter.com/theviveksuthar"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0INuFfm9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://img.shields.io/badge/Twitter-%25231DA1F2.svg%3Flogo%3DTwitter%26logoColor%3Dwhite" alt="Twitter" width="67" height="20"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ipfs</category>
      <category>tutorial</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
