0

I have a blob that I want to store as a value in a JSON: myJson = { myBlob } on IPFS. I'm working on a node.js app & using Pinata.

This is how I'm uploading the blob to IPFS:

const x = {blob: new Blob(["Hey"])};
console.log(x);
console.log(await x.blob.text());

pinata.pinJSONToIPFS(x).then((result) => {
  console.log(result);
}).catch((err) => {
  console.log(err);
});

Below is printed in the console:

{ blob: Blob { size: 3, type: '' } }
Hey
{
  IpfsHash: 'QmdEhfwmdhBazdHNcYKGAyhrFKwmkRGeWe2fyDBuXXreR3',
  PinSize: 19,
  Timestamp: '2023-03-09T09:05:28.633Z'
}

Visiting https://gateway.pinata.cloud/ipfs/QmdEhfwmdhBazdHNcYKGAyhrFKwmkRGeWe2fyDBuXXreR3 gives displays: {"blob":{}}

Now I'm retrieving from the above IPFS link:

const res = await fetch('https://gateway.pinata.cloud/ipfs/QmdEhfwmdhBazdHNcYKGAyhrFKwmkRGeWe2fyDBuXXreR3');
console.log(res.body);
const val = await res.json();
console.log(await val.blob);
console.log(await res.blob.text());

The console for the above code snippet:

{ blob: {} }
TypeError: res.blob.text is not a function

But the same works at the top: console.log(await x.blob.text());.

My question is how can I store a blob on IPFS & retrieve it as a blob? Since I need to pass the same to a different function. Also, I'm using this in a Node.js app so can't use FileReader.

NewToCode
  • 174
  • 8
  • 1
    I think you'll want to use something like base64 encoding to translate your Blob to/from JavaScript string values. JSON has no provisions for blobs as values. – Pointy Mar 09 '23 at 19:05
  • You can check on https://json.org, what JSON can contain. It can't contain a `Blob` object. If you try `console.log(JSON.stringify({blob: new Blob(["Hey"])}));` you will see what remains of your original `x`. That's exactly what then gets uploaded, and given back later. If you want to store binary data, https://github.com/PinataCloud/Pinata-SDK#pinFileToIPFS-anchor may be a better direction. – tevemadar Mar 09 '23 at 19:29
  • @tevemadar Using that function on a blob is throwing the error: `Error: filename was not provide, make sure to provide options.pinataMetadata.name`. When I add the `{pinataMetadata: { name: 'encryptedString' }}` as the second arguement in the `pinata.pinFileToIPFS()` then I get a new error: `TypeError: source.on is not a function` which seems to be an internal Pinata SDK error – NewToCode Mar 10 '23 at 09:35
  • If streaming doesn't work, and you need generic binary data, you can try https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer and then https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string – tevemadar Mar 10 '23 at 15:08
  • Your error in comments seem to be related to you not passing the data correctly to pinJSONToIPFS (https://docs.pinata.cloud/pinata-api/pinning/pin-json), you need to set `pinataContent` to your conent, you didn't do the `console.log` as suggested, your error indicates such. You can totally stringify the JSON and store it just fine. If you need to upload actual data though it's probably easier to use `pinFileToIPFS`. – Discordian Mar 14 '23 at 15:37

0 Answers0