Note

If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, 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.

 

Here is a screenshot of the resulting screen:

 Ethereum blockchain development in Infura

 

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

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: