Učite tako što ćete učiniti: lijep i lagan uvod u sustav planetarnih datoteka Inter

Primer na IPFS

IPFS je skraćenica od Inter Planetary File System. To je peer-to-peer, distribuirani sustav datoteka koji mrežu čini bržom, sigurnijom i otvorenijom. Da bismo se prebacili sa sadašnje verzije weba na distribuiranu verziju weba, potreban nam je IPFS. U osnovi je cilj zamijeniti HTTP.

NEMA centraliziranih poslužitelja. Sve je decentralizirano. Pogledajmo kako to funkcionira radeći to.

Napisao sam članak o tome kako IPFS koristi MerkleDAG, a koji možete pronaći ovdje.

KORAK 1: Instalirajte IPFS

IPFS alfa verzija napisana je na GoLang. Morat ćete ga preuzeti za svoju platformu s ove veze.

Da biste provjerili jeste li uspješno instalirali IPFS, otvorite naredbenu konzolu i unesite sljedeću naredbu.

$ ipfs help

Ako vidite nešto što započinje ovako:

USAGE
ipfs - Global p2p merkle-dag filesystem.
ipfs [--config= | -c] [--debug= | -D] [--help=] [-h=] [--local= | -L] [--api=]  ...

Onda ste spremni!

Korak 2: Inicijalizirajte IPFS čvor

Da biste inicijalizirali svoj lokalni stroj kao IPFS čvor, pokrenite sljedeću naredbu:

$ ipfs init

Ova naredba inicijalizira IPFS konfiguracijske datoteke i generira novi par ključeva pomoću 2048-bitnog RSA. To će stvoriti vaš identitet vršnjaka kao izlaz.

Vidjet ćete otprilike ovako:

initializing IPFS node at /Users/niharikasingh/.ipfs
generating 2048-bit RSA keypair...done
peer identity: QmTo1oMgGEH6Ym3H1xF55U7q4bexd5288YmEJjubDqVmKn
to get started, enter:
ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

Imat ćete drugačiji identitet vršnjaka od mog.

Sada pokrenite sljedeću naredbu da biste vidjeli pozdravnu poruku:

$ ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

Vidjet ćete rezultat koji izgleda otprilike ovako:

Hello and Welcome to IPFS!
██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗ ███████╗
██║██╔═══╝ ██╔══╝ ╚════██║
██║██║ ██║ ███████║
╚═╝╚═╝ ╚═╝ ╚══════╝
If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!
-------------------------------------------------------
| Warning: |
| This is alpha software. Use at your own discretion! |
| Much is missing or lacking polish. There are bugs. |
| Not yet secure. Read the security notes for more. |
-------------------------------------------------------
Check out some of the other files in this directory:
./about
./help
./quick-start <-- usage examples
./readme <-- this file
./security-notes

Voilà!

Korak 3: Dodajte neke datoteke u IPFS

Stvorite demo mapu bilo gdje na vašem stroju i ubacite nekoliko datoteka. Bilo koja vrsta datoteka: slike, videozapisi, glazba ... doslovno bilo što. U ovu mapu možete čak dodati još jednu mapu.

Pretpostavimo da je mapa nazvana "test-ipfs". Dakle, da biste gurnuli ove datoteke, prvo uđite u ovu mapu na naredbenom retku, a zatim pokrenite sljedeću naredbu:

$ ipfs add -r .

Ova naredba rekurzivno dodaje sve datoteke / direktorije prisutne u mapi u IPFS da bi kreirala IPFS MerkleDAG. Pomoću sljedeće naredbe možete dodati i jednu datoteku:$ ipfs add on>

This creates the following output:

added QmSTuTEThyESvDgmYdao2HK6kurXe2pqjA1KHPD8wSHVy7 test-ipfs/donut.jpeg
added QmSR9MJ5resQLjwqy7kEVVKJwTvDG53Npt9i1c6jZeZDtW test-ipfs/purse.jpeg
added QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn test-ipfs/folder1
added QmaKZ3dnc9ejBdGgEDCRtsLFNRxcY67HLjk6gXUnk9sdM9 test-ipfs

You’ll notice this long thing is the unique hash of that particular file. All the files and directories, as well as the parent folder, will have their unique hash.

$ ipfs ls QmaKZ3dnc9ejBdGgEDCRtsLFNRxcY67HLjk6gXUnk9sdM9

This works exactly like the UNIX file system. The expected output will show the following:

QmSTuTEThyESvDgmYdao2HK6kurXe2pqjA1KHPD8wSHVy7 219859 donut.jpeg
QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn 4 folder1/
QmSR9MJ5resQLjwqy7kEVVKJwTvDG53Npt9i1c6jZeZDtW 110254 purse.jpeg

The file size is shown at the end of the hash string. For example, 219859 is the file size of donut.jpeg.

Step 4: Access files online

To access files online, first we’ve got to connect our node to the IPFS network. To do that, we’ve got to run the IPFS daemon.

$ ipfs daemon

This will produce the following output:

Initializing daemon...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/169.254.100.132/tcp/4001
Swarm listening on /ip4/192.168.1.3/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmTo1oMgGEH6Ym3H1xF55U7q4bexd5288YmEJjubDqVmKn
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/169.254.100.132/tcp/4001
Swarm announcing /ip4/192.168.1.3/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

Now that you’ve pushed the files to IPFS, let’s see how can you access them.

localhost:8080/ipfs/hashOfTheFileYouWantToOpen

Suppose I wish to open purse.jpeg. I’ll copy the hash of purse.jpeg and hit the browser with the above syntax.

Likewise, you can even hear your music, video, and so on.

You can even access your content using the IPFS gateway:

gateway.ipfs.io/ipfs/hashOfTheFile

You’re about to see real magic now!

Point your browser to:

127.0.0.1:5001/webui

You’ll see this screen:

Feel free to play around.

Connections: Will show you your peers all over the world.

Files: You can push files using the webUI.

DAG: Will show you the MerkleDAG and show related info about the file when its hash is entered.

Config: This will show you configurations about your own machine.

Step 5: Access files from your peers

IPFS promises that there is no central server that is giving you the files. So what if I tell you that you can stream video from my laptop also even if IPFS daemon isn’t running on my computer?

I’ve uploaded Katy Perry’s Roar video online. You can access it from me rather than hitting YouTube.

Go to:

localhost:8080/ipfs/QmWPCbXCK4NGXKac1QoKHdW7Qqud481T5FLHzu7RnSRDGR/

And enjoy!

You’ll be streaming this video on IPFS.

You can even use VLC Media Player for this.

URL used:

//localhost:8080/ipfs/QmWPCbXCK4NGXKac1QoKHdW7Qqud481T5FLHzu7RnSRDGR/Katy%20Perry%20-%20Roar%20%28Official%29.mp4

Feel free to play around with IPFS. This was a very brief overview. But I hope you got a basic idea about what IPFS is!