1

Due to the nature of my project I have a image dataURL (NOT an actual image file) that I am trying to upload to IPFS via Pinata SDK. I have converted the image dataURL into a buffer(array) and tried 2 different methods but none of them works. Here is my code:

SAMPLE 1

var myBlob = new Blob([new Uint8Array(myBuffer)]);
var myReadableStream = myBlob.stream()
pinata.pinFileToIPFS(myReadableStream)

ERROR: Unhandled Rejection (TypeError): source.on is not a function

SAMPLE 2

var myBlob = new Blob([new Uint8Array(myBuffer)]);

var myHeaders = new Headers();
myHeaders.append("pinata_api_key", "MY_KEY");
myHeaders.append("pinata_secret_api_key", "MY_SECRET_KEY");

var formdata = new FormData();
formdata.append("test", myBlob);

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: formdata,
redirect: 'follow'
};

fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", requestOptions)
.then(response => response.text())
.then(result => console.log('result',result))
.catch(error => console.log('error', error));

ERROR: 400 Bad Request, result {"error":"Unexpected field"}

Lasanja
  • 11
  • 3

1 Answers1

1

with buffers things can be a little tricky. You'll need to format your request in a slightly different way.

I would take a look at this code snippet for an example of how somebody got this to work:

const pinataSDK = require("@pinata/sdk");
const pinata = pinataSDK(
  "Pinata API Key",
  "Pinata API Secret"
);
const { fs, vol } = require("memfs");

(async () => {
  try {
    const base64 = "base64 file string";
    const buf = Buffer.from(base64, "base64");
    memfs.writeFileSync("File Name", buf);

    const read = vol.createReadStream("File Name");

    const res = await pinata.pinFileToIPFS(read);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
})();

obo20
  • 471
  • 1
  • 4
  • 5
  • Hi, I tried replicating this and asked a few other people as well but unfortunately it is not working (Invalid request format.). Here is my code, could you kindly tell me if it works on your machine? (I will post it in a separate comment on the main thread since here it does not allow me to write above a certain character limit) – Lasanja Dec 07 '21 at 11:35
  • import dataUriToBuffer from 'data-uri-to-buffer' const svg = 'data:image/svg+xml;utf8, ' const bufferSVG = dataUriToBuffer(svg) let buffer = bufferSVG; (async () => { try { fs.writeFileSync("test", buffer); const read = vol.createReadStream("test"); const res = await pinata.pinFileToIPFS(read); } catch (error) {} })(); – Lasanja Dec 07 '21 at 11:42