Lær ved at gøre: en dejlig og nem introduktion til Inter Planetary File System

Primer på IPFS

IPFS er en forkortelse for Inter Planetary File System. Det er et peer-to-peer, distribueret filsystem, der gør Internettet hurtigere, sikrere og mere åbent. For at skifte fra den nuværende version af internettet til en distribueret version af internettet har vi brug for IPFS. I det væsentlige er målet at erstatte HTTP.

Der er INGEN centraliserede servere. Alt er decentraliseret. Lad os se, hvordan dette fungerer ved at gøre det.

Jeg skrev en artikel om, hvordan IPFS bruger MerkleDAG, som kan findes her.

TRIN 1: Installer IPFS

IPFS alfa-version er skrevet i GoLang. Du skal downloade den til din platform fra dette link.

For at kontrollere, at du har installeret IPFS, skal du åbne kommandokonsollen og indtaste følgende kommando.

$ ipfs help

Hvis du ser noget, der starter sådan her:

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

Så er du god at gå!

Trin 2: Initialiser IPFS-node

For at initialisere din lokale maskine som en IPFS-knude skal du køre følgende kommando:

$ ipfs init

Denne kommando initialiserer IPFS-konfigurationsfiler og genererer et nyt tastaturpar ved hjælp af 2048 bit-RSA. Dette vil producere din peer-identitet som output.

Du kan se noget som dette:

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

Du får en anden identitet end min.

Kør nu følgende kommando for at se hej beskeden:

$ ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

Du får vist et resultat, der ser sådan ud:

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à!

Trin 3: Føj nogle filer til IPFS

Opret en demo-mappe hvor som helst på din maskine, og smid et par filer. Enhver type fil: billeder, videoer, musik ... bogstaveligt talt alt. Du kan endda tilføje en anden mappe i denne mappe.

Antag, at mappen hedder "test-ipfs". Så for at skubbe disse filer skal du først navigere ind i denne mappe på din kommandolinje og derefter køre følgende kommando:

$ ipfs add -r .

Denne kommando tilføjer alle filer / mapper, der findes i mappen til IPFS rekursivt for at oprette IPFS MerkleDAG. Du kan endda tilføje en enkelt fil ved hjælp af følgende kommando:$ 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!