How to Run Ethereum IPFS Storage

Note

For a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:

 

Recap

In our previous article (How to Install and Start Ethereum IPFS Storage), we discussed to install and start Ethereum IPFS storage.

In this article, we learn how to run Ethereum IPFS storage.

 

Running an IPFS example

First, we will take a look at how to publish a simple page to IPFS and how to view the published content in IPFS.

Publishing a simple page in the command line

We will first create an HTML file—learn-ethereum-ipfs.html—and add some content to it, as shown in the following code block:

<html>
  <body>
  <h1>Learn Ethereum</h1>
  <h2>Hello IPFS</h2>
  </body>
  </html>

Then, we will add it to IPFS by running the following command:

ipfs add -w learn-ethereum-ipfs.html

The result is shown here. We can see the 74 B size of the file at 100% load in IPFS:

 Ethereum blockchain development in Infura

QmUnLXNyWTqh2qbmZoyfqzR8qXRBtSmFm4RCKmRf2tGj2v is the address ID that was assigned by IPFS; you can view the upload file content from the IPFS gateway: https:// gateway.ipfs.io/ipfs/QmUnLXNyWTqh2qbmZoyfqzR8qXRBtSmFm4RCKmRf2tGj2v.

 

One-to-One Live Blockchain Classes

Coding Bootcamps school offers One-to-One Live Blockchain Classes for Beginners.

 

Here is a screenshot of the resulting screen:

 

 

We just published content to IPFS through the command line. We can also do this by writing a program that publishes content to IPFS.

Publishing and querying IPFS via Infura

In this example, we will write a simple JavaScript to publish and query data to IPFS via Infura:

1. To start the project, just create a project folder. Navigate to the project folder and run the following command to initialize a node IPFS project:

npm init -yes ipfs-mini

2. Create the publish side node file, PublishData.js, as shown in the following code block. First, we use the ipfs-mini library and connect to https://infura via ipfs.infura.io:5001. Then, we call ipfs.add to add data to IPFS:

const IPFS = require('ipfs-mini');
  const ipfs = new IPFS({host: 'ipfs.infura.io', port: 5001, protocol: 'https'});
  const myData  = "Hello IPFS Infura"; ipfs.add(myData, (err, data) => {
  console.log("IPFS HASH:", data);
  });

We will see that the hash value is returned after the ipfs.add function, like so:

 Ethereum blockchain development in Infura

 

We will use the return hash to search for the data on the query side.
3. Write QueryData.js on the query side to get the published data from IPFS:

const IPFS = require('ipfs-mini');
  const ipfs = new IPFS({host: 'ipfs.infura.io', port: 5001, protocol: 'https'});
  const ipfsHash  = "QmRF2XAkJbQYNQYYscr7ctUkhEUdyFWQfwBdXQiZaWgMyF"; ipfs.cat(ipfsHash, (err,  data) => {
  console.log("Received:", data);
  });

Similar to PublishData.js, we set up the initial IPFS connection by using the ipfs-mini library. We call the ipfs.cat function to retrieve the published IPFS data. The result is as follows:

 Ethereum blockchain development in Infura

 

Congratulations! You just learned how to publish your own data to decentralized storage—IPFS. In the next section, we will quickly review another similar decentralized storage—Swarm.

 

Next Article
In our next article (How to Install Ethereum Swarm Storage), we discuss how to work with Ethereum Swarm storage.

This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.

 

Resources

coming soon