Upload file with json to ipfs (best way ? )

I have a file which I want to upload to ipfs, but with the file, i also want to upload a string. So, JSON seems to be a good way.

const finalData = {
   title: 'title here',
   data: HTMLInputFileElement

Question 1. Can I pass this data to ipfs and get one cid ? the problem is that I think the file in data property won’t be uploaded and this is a problem. So The only choice I have is that first I upload the file, get the CID, put the CID inside data key and then upload finalData again to ipfs and get another cid.

The problem is that when I query CID, I have to first get a json and then query ipfs again for data in order to get a file. Isn’t there a better way not to do this 2 things ? is there any other choice to achieve what I want ?

Aha I do this in my demo: http://portal.thedisco.zone/testing/paste_bundle

// add the data we want to store
addRecv = await ipfs.add('data here');
cid = addRecv.path;

// create the metadata object we'll be storing
metaObj = {"fname": "myfile.txt", "lang": "plain-text", "cid": cid};
jsonObj = JSON.stringify(metaObj);

// add the metadata itself as well
metaRecv = await ipfs.add(jsonObj);

The CID you’d want is in metaRecv.path. So with ipfsbin, it takes the metadata CID, and then loads the CID embedded in that in the background.

I think this way is decent as you can still utilise de-duplication (if someone uploads object without metadata, they help in re-sharing that data). However if you want, using a format other than JSON you can likely store more data types, just all in one blob. IE: You can store binary data in IPFS.

What @Discordian posted is probably what you want to be doing, and the best answer, but this also made me realize technically BSON could do this all in one CID. In very special unusual circumstances BSON might be desirable. Like for example MongoDB uses BSON, so persisting MongoDB records themselves might make sense to do as BSON.

1 Like

I actually use BSON in LevelDB all the time, this makes a lot of sense to me. Save a little space (I’d still likely link to another CID though).

Right, and the big drawback to packing binary data into a BSON would be the cases like where it’s an image or video, etc where other apps and APIs will need to access it directly, and won’t know anything about any proprietary (BSON) formats or how to extract out from those.

1 Like

I get what you are doing there, but I find that kinda hacky, if someone where to pin metaRecv’s CID, it wouldn’t pin addRecv’s too. Either yuo setup your own ipld sector, but it’s probably too much work.
I would use unixfs instead, create 2 files, one with your data, and one meta one, and wrap both in an unixfs folder, then access either Qmfoo/meta.json or Qmfoo/data.

1 Like


  1. I think pinata has a really good feature - Pinata | Effortless IPFS File Management
  2. @wclayf is there any good resource where i can see how to get BSON from my file and some metadata ? Really couldn’t find a good explanation.
  3. @Jorropo about UnixFs. When something goes into ipfs, IPLD gets created with data + links. Isn’t this the same thing as UnixFs ? there’s not at all a good documentation how they are different at all.

@novaknole BSON is just a variant of JSON that’s able to hold byte arrays, but I don’t know of any ‘standardized’ way to use it to store files.

I like this idea, I’m going to do this. Thank you :slight_smile:.