DEV Community

Robert
Robert

Posted on • Edited on

How to add images to IPFS with node.js and ipfs-core

What is IPFS?

IPFS stands for InterPlanetary File System, is a p2p protocol for storing and sharing data in a distributed file system.

IPFS don't rely on a server so it makes it decentralized. so it is easy to deploy and use. which is a good thing for web3 as it is a decentralized protocol.

IPFS is not jut for blockchain developers, its also for web developers, content creators, etc.

There are some terms that are used in IPFS.

node -> think of it as a server, every node is a server and every user is running a node.

pin -> you need to pin the files to guarantee the content access is always available to the user, there are pinning services like Pinata or Infura, if you don't pin your files, they will be deleted after a certain time.

CID (Content ID) -> is a unique identifier for a file, think of it as a hash/fingerprint of the file.

I encourage you to read the IPFS documentation to learn more about IPFS.

You can contact me by telegram if you need to hire a Full Stack developer or if you need a discord bot for your server.

You can also contact me by discord Appu#9136.

You can clone the repo if you want.

Prerequisites

Creating Our Project

  1. open your terminal and type the following
  2. mkdir node-ipfs-tutorial
  3. cd node-ipfs-tutorial
  4. npm init --y
  5. code .

Dependencies

To install dependencies go to your project folder open a terminal and type the following.



npm i ipfs-core


Enter fullscreen mode Exit fullscreen mode

Now go to your package.json and add this, we will be using import so we will add "type" : "module"



"type" : "module",
"scripts": {
  "start": "node ./src index.js"
},


Enter fullscreen mode Exit fullscreen mode

Project File Structure

node-ipfs-tutorial/
├── node_modules/
├── src/
│ └── index.js
└── package.json

Table of Contents

  1. Hello World
  2. Adding Images
  3. Retrieving Data
  4. Conclusion

1. Hello World

Let's start with the example from the ipfs-core documentation.



import * as IPFS from 'ipfs-core'

const ipfs = await IPFS.create()
const { cid } = await ipfs.add('Hello world')
console.log(cid)


Enter fullscreen mode Exit fullscreen mode

Now type npm start in your terminal and you will see something like this.

console message

Open your browser and type https://ipfs.io/ipfs/<cid> and you will see the content of the file.

Hello world

2. Adding Images

Now we're going to add some images, for this example I will use free images from unsplash.

So will create a folder named images and put the images in it, I added 3 images.

Images folder

So I will import fs because will work with files and another directory, then will specify in a const where is the images folder and read it.

After that will use a for loop read each file inside the images folder and add it to the IPFS.

This time I am using result to show all the properties of the added file



import * as IPFS from 'ipfs-core'
import fs from 'fs'

const imagesDir = './src/images'

const files = fs.readdirSync(imagesDir)

const ipfs = await IPFS.create()

for(let file of files) {
  const buffer = fs.readFileSync(`${imagesDir}/${file}`)
  const result = await ipfs.add(buffer)
  console.log(result)
}


Enter fullscreen mode Exit fullscreen mode

You will see something similar to this.

console result

3. Retrieving Data

To simplify the tedious task of copying the cid and pasting it into the browser, and add https://ipfs.io/ipfs/.

Let's create a const gateway with https://ipfs.io/ipfs/, and then after get the result console.log(gateway+result.path) like this.



import * as IPFS from 'ipfs-core'
import fs from 'fs'

const imagesDir = './src/images'

const files = fs.readdirSync(imagesDir)
const gateway = 'https://ipfs.io/ipfs/'
const ipfs = await IPFS.create()


for(let file of files) {
  const buffer = fs.readFileSync(`${imagesDir}/${file}`)
  const result = await ipfs.add(buffer)
  console.log(result)
  console.log(gateway+result.path)
}


Enter fullscreen mode Exit fullscreen mode

Now to visit the images you can open your browser and type https://ipfs.io/ipfs/<cid> or just ctrl + click the link and you will see the content of the file.

Don't worry if you get this error.

error 504 gateway time-out

You can use alternative ways to retrieve the data such another gateway like https://gateway.pinata.cloud/ipfs/.

picture

4. Conclusion

We learned how to make add content to IPFS with node.js using ipfs-core. For my next post I plan to make an example using the Pinata API.

I really hope you have been able to follow the post without any trouble, otherwise I apologize, please leave me your doubts or comments.

You can contact me by telegram if you need to hire a Full Stack developer.

You can also contact me by discord.

You can clone the repo if you want.

Thank you for your time.

Top comments (0)