Tutorial - Get Ready to Use IPFS Cluster

Tutorial - Get Ready to Use IPFS Cluster

IPFS has become one of the most popular ways to store your data in a decentralized way. This tutorial will demonstrate the basic usage of ipfs, ipfs-cluster and how to access it via axios in JavaScript.

Perquisites

  • Download ipfs,ipfs-cluster-service on all of your servers and ipfs-cluster-ctl on at least one of your servers on IPFS distributions. For me, I download them on all of my servers.

  • Make them into PATH like

    ln * /usr/bin && chmod a+x *
  • make sure the following port is accessible on your server

    • 8080 (ipfs get file)
    • 9094 (ipfs cluster service RESTful API)
    • 9096 (ipfs cluster service peer commuication)

Configuration

The following steps should be done on all of your servers

  • Init ipfs

    ipfs init
    • (optional) Go to the .ipfs directory it generated and open config

      Change to "Gateway": "/ip4/0.0.0.0/tcp/8080" so that we can get the file from http://ip:8080/hash

  • Init ipfs-cluster

    ipfs-cluster-service init

    Go to the .ipfs-cluster directory it generated and open service.json

    Make sure every server has the same secret

    • (optional) Go to the .ipfs directory it generated and open config

      Change to "http_listen_multiaddress" in restapi to "/ip4/0.0.0.0/tcp/9094" so that we can add&pin the file into the whole cluster from http://ip:9094

Start Daemon

The following steps can be optimized to use superviser

  • Start ipfs daemon on every server

    ipfs daemon
  • Start ipfs-cluster-service daemon on first server

    ipfs-cluster-service daemon
  • Start ipfs-cluster-service daemon on other servers by bootstrap

    ipfs-cluster-service daemon --bootstrap /ip4/[ip-of-first-server]/tcp/9096/ipfs/[cluster-service-id]

    cluster-service-id can be inferred from cluster.id from service.json or logs of ipfs-cluster-service daemon on first server.

    • e.g

      ipfs-cluster-service daemon --bootstrap /ip4/12.34.56.78/tcp/9096/ipfs/QmVw41GTKr76Xw2zAgXn1ic98nZfYj1DPd7VJPku4q1oW9

Right now, they should be connected!

Access cluster by axios

  1. add&pin files on one of the server in the cluster

    POST [ip]:9094/add (request content-type must bemultipart/form-data )

  2. get files from all of the server in the cluster

    GET [ip]:8080/ipfs/[CID]

Here is the axios example in Vue to upload a binary file (Buffer) in one peer and access it from another in frontend.

let buffer = Buffer.from[1,2,3];
let bodyFormData = new FormData();
const blob = new Blob([new Uint8Array(buffer)]);
bodyFormData.set('file', blob);
let that = this;
this.$axios({
method: 'post',
url: 'http://12.34.56.78:9094/add',
data: bodyFormData,
config: {
headers: {'Content-Type': 'multipart/form-data'},
}
}).then(response => {
const cid = response.data.cid;
that.$axios.get(`http://23.45.67.89/ipfs/${cid}`, {
responseType: 'arraybuffer'
}).then(x => {
const downloaded = Buffer.from(x.data);
console.log(downloaded)
})
})
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×