IPFS for Beginners - Storing Data in a Distributed Hash Table

in #amp-ampampampampltearn5 years ago (edited)


When we try to build a DApp, we can deploy our code on the blockchain. Also, we can store all the data on blockchain to made data decentralize. However, most blockchain platforms are not meant to handle massive amounts of data.

For Ethereum, the full node is around 2TB now. Imagine we store all our videos on the Ethereum, it could cost a lot space on Ethereum blockchain.

If you have ever deploy your program on AWS, you may have experience to deploy your code on the EC2 VM. When you store all your data on the same VM, you may find your disc get full very quickly. It cost you a lot for your AWS fee. What most developer will do is to store data in another service called S3.

Are there any decentralized data system? The answer is yes. The most famous one is InterPlanetary File System (IPFS) Its an improvement of BitTorrent.

There's a book tell more ideas about the role of IPFS. You can get it on Amazon.

https://amzn.to/2vurfMV

Now, let us have hand on experience of IPFS. What will be in this example, we will upload our website LearnThings.Online on the IPFS, and visit it via HTTP.

To prepare, First, download our website. You can do it in the console by

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent learnthings.online

You can see a folder called "learnthings.online"

Second, install IPFS on your computer. https://docs.ipfs.io/guides/guides/install/

If you install it successfully, you can start the daemon by

ipfs daemon

We can see Daemon is ready. Now open another console window. We add the folder learnthings.online to IPFS by

ipfs add -r learnthings.online

We can see, each files & folders will get their own CID

The folder "learnthings.online" have CID "QmZg48VEAjMHSTweE8FnvEcuDbogRQBPVR5vsTrMnBiMkV"

And we can see the file on the browser via

http://localhost:8080/ipfs/QmZg48VEAjMHSTweE8FnvEcuDbogRQBPVR5vsTrMnBiMkV/

We can see, it show on our localhost. Can other people see this? Actually, there are some IPFS gateways to help people to check the file. See https://ipfs.github.io/public-gateway-checker

We use dwb.link to check the origin file.

We can check the file via http://dweb.link/ipfs/QmZg48VEAjMHSTweE8FnvEcuDbogRQBPVR5vsTrMnBiMkV (It take time when we first load it.)

There's one problem arise. How can we update the file? It come into the tool IPNS.

Now, generate an IPNS link.

ipfs/QmZg48VEAjMHSTweE8FnvEcuDbogRQBPVR5vsTrMnBiMkVWe know we can visit our file through We can check the file via http://dweb.link/

So, what's IPNS link? What it means "Published to QmedPEKLwDxZo6TQ9BXWVAAxFPxWA9AACdu1PmX6PX36d"?

ipns/QmedPEKLwDxZo6TQ9BXWVAAxFPxWA9AACdu1PmX6PX36dLet's visit http://dweb.link/

We can see the same thing. Actually, the CID "QmZg48VEAjMHSTweE8FnvEcuDbogRQBPVR5vsTrMnBiMkV" will change when we update the file.

Let's see, what will happen if we change the file about.html?

We found the CID for about.html & learnthings.online changed. And other files are the same with above.

Now, get the IPNS Link again, we found it get the same IPNS. It means we can get same link with IPNS even we update the file.

Till now, we know how to put files on IPFS, check files through HTTP, update file. But there are some important question. Will the file always remain exist on IPFS network? The answer is no, there's no incentive for people help you to store files. Protocol Labs, who build IPFS, try to build a blockchain system to implement the IPFS called Filecoin. The test-net of Filecoin is online now.

There are some materials you can check if you like to go deep.

Decentralized Applications: Harnessing Bitcoin's Blockchain Technology

FileCoin

Understanding IPFS in Depth



Posted from LearnThings.OnlineLearn Things Online : http://learnthings.online/course/2020/03/17/ipfs-for-beginners-storing-data-in-a-distributed-hash-table