Main Category

Beginner’s Guide to IPFS

Let’s start with a basic description of IPFS:

IPFS is a distributed system that allows you to store and access files, webpages, apps, and data.

What precisely does that mean? Assume you’re conducting a study on Leafy seadragon. Did you know they are classed as a Protected Species in South Australia and have no known predators? To begin, go to the Wikipedia page on seadragon at:

When you enter that URL into your browser’s address bar, your computer requests the Leafy seadragon page from one of Wikipedia’s computers, which may be on the other side of the country (or perhaps the world).

However, it isn’t your only choice for fulfilling your Leafy seadragon demands! There is an IPFS mirror of Wikipedia that you might use instead. If you utilize IPFS, your computer will request the Leafy seadragon website as follows:

/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco/wiki/Leafy seadragon.html

IPFS can discover that delectable Leafy seadragon information based on its contents rather than its location. The IPFS-ified version of the Leafy seadragon information is represented by the string of numbers in the middle of the URL (QmXo…), and rather than asking one of Wikipedia’s computer systems for the page, your computer uses IPFS to request a large number of computers all over the world to share the page with you. It can obtain Leafy seadragon information from anyone, not only Wikipedia.

When you utilize IPFS, you don’t merely get things from somewhere else; your computer also contributes to their distribution. When your neighbor or anybody using IPFS wants the same Wikipedia article as you, they may be just as likely to obtain it from you as they are from your neighbor or anybody using IPFS.

IPFS enables this for any type of file a computer may store, such as an email, a document, or even a database record.

There are various IPFS distributions and installation methods. However, this article will be using the cross-platform command-line interface program. The command interface is extensively documented and broad. Don’t be intimidated by the lengthy number of instructions; you’ll only need a few easy ones to get started with IPFS.

You must first generate a peer ID before you can use it. Open a terminal and type the following command:

ipfs init 

The significance of that command and its result will be described in greater detail later; for now, simply go to the next step.

Publication of an Image File:

Let’s see if we can post this PNG image. Once you’ve saved it as ipfs-logo.png, launch a terminal in the directory where it is stored and type the following command:

ipfs add ipfs-logo.png

The result will be as follows:

added QmbYq2pMi91Xd5Hu6Z1edrvP4BwJXCH9HhRX8Tk99DJWG6 ipfs-logo.png

A multihash is a lengthy string that begins with Qm… It is a one-of-a-kind identifier derived from the file’s contents. No matter when and how often a file is rereleased, it will always be the same for that particular file.

Obtaining the File:

It’s just as easy to get the file back:

ipfs get QmbYq2pMi91Xd5Hu6Z1edrvP4BwJXCH9HhRX8Tk99DJWG6 --output out.png

The – -output parameter allows you to provide the name of the downloaded file.

Creating a Directory:

You may even publish an entire directory with files and nested directories simultaneously. To do so, add the -r (short for — recursive) parameter to the add command. Place the logo file in the logo directory, then run:

ipfs add -r logo

The result will be as follows:

added QmbYq2pMi91Xd5Hu6Z1edrvP4BwJXCH9HhRX8Tk99DJWG6 logo/ipfs-logo.png 
added QmU1muwAeYjHX1kUnYEXPWEhnFxcVGS6wv8tggoHLHkm3f logo

The directory’s identification appears on the bottom line. It may be accessed using ipfs get, just like a single file.

Each file in the directory may be identified by its relative path to the parent directory. Each file, however, has been granted a unique identification. Without any directory context, a specific file can be accessed by its multihash. In our example, the following commands should return the same file without fetching the whole directory:

ipfs get QmU1muwAeYjHX1kUnYEXPWEhnFxcVGS6wv8tggoHLHkm3f/ipfs-logo.png 
ipfs get QmbYq2pMi91Xd5Hu6Z1edrvP4BwJXCH9HhRX8Tk99DJWG6 --output ipfs-logo2.png

Embracing the Swarm:

You’ve just published some files to your local IPFS storage and retrieved them. However, your file is not yet accessible to the entire world. To accomplish this, you must first launch an IPFS node:

ipfs daemon

The node functions as both a server and a client. It will link to several other nodes and share information on available material. You can see which nodes are related to you by entering:

ipfs swarm peers

The result will include several lines that like the following (the exact addresses and hashes may vary):

/ip4/ /ip6/2604:a880:cad:d0::17:2001/tcp/4001/ipfs/QmUR8d2WLbNcAFRMWn3SMdBRDhJugZUezfwLkDYti3Gc3w

Each line represents an IPFS node’s multiaddress. It comprises an IP network location (address and port) and a distinctive peer identification. The node’s address may change (when your laptop goes from place to place to café, for example), but the peer ID remains constant.

Data Storage:

No one node can conceivably store all of the data ever released. This indicates that your node may opt to discard some of the data. This also implies that you cannot rely entirely on your peers: if no one is interested in retaining your data, it may just vanish from the network.

You can pin the data object’s identification to prevent it from vanishing. This ensures that the data is not erased if your local node decides to clear up some space.

Because the files you’ve uploaded are automatically pinned, let’s pin something you don’t have yet:

ipfs pin add /ipfs/QmNhFJjGcMPqpuYfxL62VVB9528NXqDNMFXiqN5bgFYiZ1/its-time-for-the-permanent-web.html

The result should be as follows:

pinned Qmcx3KZXdANNsYfSRU1Vu4pchM8mvYXH4N8Zwdpux57YNL recursively

This process also downloads the data to your computer to ensure that it never disappears. However, recovering it should be a breeze now:

ipfs get Qmcx3KZXdANNsYfSRU1Vu4pchM8mvYXH4N8Zwdpux57YNL -o article.html

How to Find the Right Data:

Despite the fact that the file was just 26 kB in size, the last action may have taken a while. This occurs because data must first be identified until it can be saved.

The requested data block might be stored on any IPFS node worldwide. Because your local node is unlikely to maintain direct links to every other server or keep a record of every block added elsewhere, locating the correct node might take some time.

The information about which node holds which blocks is arranged in a distributed hash table, which is spread among nodes in the same way as data is. When searching for data specified by a specific hash, your node must first discover the node with that block. The node requests several of its immediate peers, so if one of them happens to have the block in question, the search is over. If a peer does not hold the data, it makes the identical query to its peers until the data block’s keeper is discovered.

The network’s nodes are configured so that this procedure has minimal overhead, and the entire network may be traversed in minutes. However, in the worst-case scenario, the search may take several minutes. This is true for recently released data, and knowing your colleague seated next to you published it can be aggravating. Fortunately, you can avoid the worldwide search if you already know where to seek it.

Recall the Qm… hash that the init command printed out? That was your node’s peer ID, after all. When you type the command, it will be presented with some more information, so don’t worry if you forgot to write it down.

ipfs id

The resulting JSON object will include various fields. For the time being, the most significant is the node ID.

If you know the node ID that must hold the data you want, you may bypass the time-consuming search by providing a direct connection with that node. To accomplish this, use the command:

ipfs swarm connect /ipfs/Qm…

replacing the node’s ID for the Qm… path

Your IPFS node must first look for a new peer before connecting. This step can also be skipped if you know the distant node’s entire multi-address. In such a scenario, you may run the same command with the complete multiaddress as an input, as seen below:

ipfs swarm connect /ip4/<IP address>/tcp/<port number>/ipfs/<peer ID>

IPFS may operate over various network protocols, and a node often listens on numerous network interfaces.  As a result, a node will often have many multi addresses with somewhat varied forms.

Each one contains the peer ID and information on how it may be accessed (e. g. an IPv5 address and port).

You may also retrieve the node’s addresses by using its peer IDs:

ipfs dht findpeer Qm...

When a Peer Connection Fails:

It’s possible that your colleague at the workstation next to yours has uploaded a data block — say, a new Fury build — but you can’t seem to get it or even connect to his IPFS node. The most common reason for these problems is a network connection, such as a firewall that stops machines on the very same network from communicating with one another. The following procedures will assist you in determining the root of the problem.

Attempt to access the data using a WWW gateway. The IPFS logo, for example, described at the start of this article, may be seen at this URL:

This usually takes several minutes for newly released data. If, however, the request expires, one of these things happens:

The node(s) that used to hold the data block is presently offline, or the node that published the block is disconnected from the entire network, maybe due to a network firewall.

Whether the gateway successfully retrieves the data, but the local IPFS node did not use telnet to see if you can access the peer’s address and port. You’re out of luck if the connection could be formed. Data sharing is still feasible, but not in all cases, only through a third node that you and your peer can access. You have two options for resolving the issue:

Speak with your network administrator about allowing IPFS connections on your local network.

Hosting your data at one of the pinning providers may move it outside the limited network.