IPFS made simple with web3.storage

IPFS made simple with web3.storage

A demo to get started with web3.storage

ยท

3 min read

What is IPFS?

Inter Planetary File System known as IPFS is a peer-to-peer decentralized file sharing system. It's a better and effective way to store and access data be it files, images, videos or even websites and applications.

What does IPFS mean in the web3 world?

You are building a decentralized application which runs on blockchains, there's no centralized entities comes into picture. Why rely on centralized systems when it comes to storing data? Store data on IPFS and make your applications fully decentralized !!

Now the next question you will have in mind is, How to use IPFS? ๐Ÿค”

How to use IPFS?

To use it, you are required to access a node through which you can store and access the data. Easy!!

But how to access a node?

  • Run your own IPFS node and leverage it. This requires few things to be downloaded and setup. Looks time taking?๐Ÿ˜ช Use the second option.

  • Utilizing the best and easiest solution available to developers . One of them is web3.storage. which makes using IPFS smoother, efficient and allows you to build projects/products faster.

What is web3.storage?

  • It is a decentralized storage service which makes uploading data to Filecoin and IPFS easily through the api's or the web user interface provided by them. It is super easy to use and works in a trustless environment. What trustless means is that the user can check the files are indeed theirs by cryptographically verifying that the CID corresponds to their data or not.

web3.storage provides upto 1 TB fee storage!!

How to use web3.storage in your projects?

You just need three simple steps to do this!!

  1. create an account on web3.storage and create an api token.

  2. installing the web3.storage library using npm or yarn, whichever you like using

     // installing through npm
     npm install web3.storage
    
     // or using yarn
     yarn add web3.storage
    
  3. Few lines of code ๐Ÿ˜„
  • First set the api token in an environment variable.
  • Then call the api's and functions

    import { Web3Storage } from 'web3.storage'
    
    // Create a web3.storage client
    const MakeStorageClient = () => {
      return new Web3Storage({token: process.env.WEB3STORAGE_TOKEN})
    }
    
  • Send your data!!

    // Send data to IPFS
    const saveToIpfs = async (files) => {
      const client = MakeStorageClient()
      const cid = await client.put([files])
      console.log(`File stored with CID: ${cid}`)
      return cid
    }
    
  • Retrieve data using CID.

    const retriveData = async (cid) => {
      const client = MakeStorageClient()
      const res = await client.get(cid)
      console.log(`Got a response! [${res.status}] ${res.statusText}`)
      if (!res.ok) {
        throw new Error(`failed to get ${cid} - ${res.statusText}`)
      }
    
      // retrieved file objects from the response
      const files = await res.files()
      for (const file of files) {
        console.log(`${file.cid} -- ${file.path} -- ${file.size}`)
      }
    }
    

That's it!! Enjoy building projects with web3.storage.

Well that's my first blog here. A lot more to come and I will get better at writing each time. Follow me here as well as on twitter @aayushdeshmukh where I share my journey learning various thing about web3 and blockchain. Thank you for your time.

ย