How to add files using kubo-rpc-client

Hello :wave: all

You can use the kubo-rpc-client Javascript library to add files from a local machine. In order to do so, please follow the below steps:

Install the library from the official kubo-rpc-client repository. Note that if you’ve previously used the ipfs-http-client to upload content to IPFS, it was deprecated and we recommend using the kubo client going forward.

Also keep in mind that the kubo client can only be imported as a module, so for simplicity save the below piece of code as an mjs file, index.mjs for example, before running it in node.

Still if you wish to use .js files instead .mjs you will need to add the top-level field “type” with a value of “module” in the nearest package.json file as explained here.
You can also create an empty package.json file in your project directory and add the below to get it working as a .js file.

Prerequisites: Use your own Project ID and Project Secret.

Please note that you have to create a folder structure with your files in it. In my example my folder structure looks like: docs/hello.txt

import { urlSource, create } from 'kubo-rpc-client'

const projectId = '23...XXX';
const projectSecret = '23...XXX';
async function addFile(){
	const auth =
	  'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')

    const client = await create({
        host: 'ipfs.infura.io',
        port: 5001,
        protocol: 'https',
        headers: {
          authorization: auth
        }
      })
 
      for await (const file of client.addAll(globSource("./docs", "**/*"))) {
        console.log(file)
      }
}

addFile()
3 Likes

Hello @Flaveeu ,

Thanks a lot for the example.

I tried your sample and it works fine when having nodejs 16.0.0, but with nodejs 18.0.0 i get this error

node:internal/deps/undici/undici:7881
            fetchParams.controller.controller.error(new TypeError("terminated", {
                                                    ^

TypeError: terminated
    at Fetch.onAborted (node:internal/deps/undici/undici:7881:53)
    at Fetch.emit (node:events:527:28)
    at Fetch.terminate (node:internal/deps/undici/undici:7135:14)
    at Object.onError (node:internal/deps/undici/undici:7968:36)
    at Request.onError (node:internal/deps/undici/undici:696:31)
    at errorRequest (node:internal/deps/undici/undici:2774:17)
    at TLSSocket.onSocketClose (node:internal/deps/undici/undici:2236:9)
    at TLSSocket.emit (node:events:539:35)
    at node:net:715:12
    at TCP.done (node:_tls_wrap:581:7) {
  [cause]: TrailerMismatchError: Trailers does not match trailer header
      at Parser.onMessageComplete (node:internal/deps/undici/undici:2142:35)
      at wasm_on_message_complete (node:internal/deps/undici/undici:1778:34)
      at wasm://wasm/0002afd2:wasm-function[45]:0x8dc
      at wasm://wasm/0002afd2:wasm-function[56]:0x1ad3
      at wasm://wasm/0002afd2:wasm-function[55]:0xcd7
      at wasm://wasm/0002afd2:wasm-function[21]:0x4e4
      at Parser.execute (node:internal/deps/undici/undici:1880:26)
      at Parser.readMore (node:internal/deps/undici/undici:1859:16)
      at TLSSocket.onSocketReadable (node:internal/deps/undici/undici:2189:14)
      at TLSSocket.emit (node:events:527:28) {
    code: 'UND_ERR_TRAILER_MISMATCH'
  }
}

Here are my steps:
mkdir testIpfs
cd testIpfs
npm init -y
npm i ipfs-http-client
touch index.js

PS: i added : “type”: “module”, in my package.json
and added docs folder with hello.txt file.

here is my index.js (same as your code actually)

import { globSource, create } from 'ipfs-http-client';

const projectId = 'xxx';
const projectSecret = 'xxx';
async function addFile(){
	const auth =
	  'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')

    const client = await create({
        host: 'ipfs.infura.io',
        port: 5001,
        protocol: 'https',
        headers: {
          authorization: auth
        }
      })
      for await (const file of client.addAll(globSource("./docs", "**/*"))) {
        console.log(file)
      }
}


addFile()

If you have any idea how to fix this please let me know!
thanks in advance!

There is also this open github issue that i found that seem to have same problem here

1 Like