Storing/Retrieving file over Blockchain by leveraging IPFS and Node.js
by
on April 28th,2017

Blockchain is becoming hot topic in 2017 and lot of use cases being developed over Blockchain. There are new platforms getting introduced in the market to solve different problems over Blockchain. Recently we have been developing solution where it required to store and retrieve important files over Blockchain, and we came across IPFS as solution to integrate. With this blog, we would like to share some knowledge on how can we integrate IPFS with Node.js for this purpose.

We are not going into deep with “What is Blockchain?” or “What is Node.js?” as we believe that who are interested to read this blog must be aware about those things. However, we will give you quick overview about IPFS in the next section.

What is IPFS?
IPFS stands for InterPlanetary File System, is hypermedia distribution protocol. It’s a distributed file system with characteristics of Blockchain to store and retrieve the files. It is an open source platform that complements or replace HTTP protocol.
How it works?
It generates cryptographic hash for each file and we can find the content of the file from any of the node in network using that hash. Most significantly IPFS removes duplications across the network and track version history for every file.
To read more about IPFS CLICK HERE. One of the alternative of this is Swarm (Based on Ethereum).
Integration flow
111
Prerequisites
Node.js – Should be greater than V4 (Refer this Installation Guide)
IPFS – (Refer this Installation Guide)
Steps
1) First step is to add IPFS dependency in Node.js project. Run this command

npm install --save ipfs-api

2) Code with Node to import IPFS and connecting to IPFS host.

//Step 1. Import dependency in project
var ipfs = require('ipfs-api')();
//Step2. Connect to IPFS daemon API Server.
var ipfs = ipfsAPI
('localhost', '5001', {protocol: 'http'})
// leaving out the arguments will default to these values
//Step2. Or Connect using Multiaddr
var ipfs = ipfsAPI
('/ip4/127.0.0.1/tcp/5001')
//Step2. Or Connect using options
In case you are getting this error “cors origin is not allowed”, set following configuration.
> ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"
> ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
> ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"

3) There are different ways we can store files into IPFS.

//1. Add files from file system or path to IPFS
ipfs.util.addFromFs(path, option, callback)
For example,
ipfs.util.addFromFs
('path/.../.../folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
if (err) {
throw err
}
console.log(result)
})
//2. Add a file from Folder to IPFS
ipfs.util.addFromFs(path, option, callback)
For example,
ipfs.util.addFromFs
('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
if (err) {
throw err
}
console.log(result)
})
//3. Add a file from a URL to IPFS
ipfs.util.addFromURL(url, callback)
For example,
ipfs.util.addFromURL
('http://xyz.com/', (err, result) => {
if (err) {
throw err
}
console.log(result)
})
//4. Add a file from stream to IPFS
ipfs.util.addFromStream(stream, callback)
For example,
ipfs.util.addFromStream(, (err, result) => {
if (err) {
throw err
}
console.log(result)
})

4) Handling response. Once we run the command to add file, IPFS send JSON response in the following format,
[
{
path: 'test-folder',
hash: 'QmRNjDeKStKGTQXnJ2NFqeQ9o
W23WcpbmvCVrpDHgDg3T6',
size: 2278
},
// ...
]

This is just an example, in your response values of these attributes will be different.
5) Retrieve the file. You can store the Hash for each file in your database or file system to retrieve the file in following way from any of IPFS Node.
localhost:9001/ipfs/QmRNjDeKSt
KGTQXnJ2NFqeQ9oW23WcpbmvCV
rpDHgDg3T6

Where localhost would be replaced by your host name and port too. Last parameter of this URL is the hash of the file that is generated by IPFS in step 4 and we stored in our database.
Conclusion
We hope that this blog will help you get quick idea about how to integrate IPFS with node, to store or retrieve the files over Blockchain. Feel free to reach out us marketing@minddeft.com for further queries.
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Reach us to know how we can solve your problems?
B2/1209, Palladium, Behind Divya Bhaskar Press, Corporate Road, Prahalad Nagar, Ahmedabad - 380015
+91-79-40192627
+1 (415) 968 5466