I am trying to upload the metadata(JavaScript Object) of a file already uploaded to IPFS, from the client side in the NextJS. I am able to upload files(PDFs) to IPFS using the Infura's HTTP API
with the help of the ipfs-http-client
package, but I am not able to do so with JSON?
I have also tried uploading using the pinata SDK
but in vain.
How can I upload the JavaScript object to IPFS?
The Short Answer: Perform JSON.stringify()
on the JavaScript Object and upload the returned value to the IPFS. You can either use Infura's HTTP API end-point or ipfs-http-client
which is a client library for the IPFS HTTP API.
The Long Answer:
ipfs-core
package:yarn add ipfs-core
import * as IPFS from 'ipfs-core'
const ipfs = await IPFS.create() //creating an IPFS node
//passing the file object extracted from the HTML input.
const { cid } = await ipfs.add(file)
console.info(cid) // QmXXY5ZxbtuYj6DnfApLiGstzPN7fvSyigrRee3hDWPCaf
JSON.stringify(<JS_OBJECT>)
and upload the returned value of this function to the IPFS.