[SOLVED] File upload by public is disabled with node js

Hi!

I have this node js server that runs, atm locally, but will run on heroku/moralis (still to be decided). It has no front-end but executes code to upload a file to ipfs upon contract events.

It works like so: token gets minted > cloud function saves the event > nodejs gets triggered upon new record entry.

Code is as follows:

const Moralis = require("moralis/node");

Moralis.initialize = "MY-APP-ID"
Moralis.serverURL = "MY-SERVER-URL"
const btoa = function (str) {
  return Buffer.from(str).toString("base64");
};

init = async () => {
  console.log("I have been summoned!");
  let query = new Moralis.Query("NewTokenMints");
  let subscription = await query.subscribe();
  subscription.on("create", onTokenMint);
};

onTokenMint = async (token) => {
  console.log("New Token Minted!");
  console.log(token.attributes.tokenID);
  const metadata = {
    name: "A collection of Kekws",
    Id: token.attributes.tokenID,
  };

  const metadataFile = new Moralis.File(token.attributes.tokenID + ".json", {
    base64: btoa(JSON.stringify(metadata)),
  });
  await metadataFile.saveIPFS();
  console.log(metadataFile.ipfs());
};

init();

But when doing the save to ipfs function it gives the following error:

(node:41668) UnhandledPromiseRejectionWarning: Error: File upload by public is disabled.
15:11:34 web.1 | at handleError (D:\2 Programming\KekwEventListener\node_modules\moralis\lib\node\RESTController.js:433:17)
15:11:34 web.1 | at processTicksAndRejections (internal/process/task_queues.js:95:5)
15:11:34 web.1 | at async Subscription.onTokenMint (D:\2 Programming\KekwEventListener\index.js:28:3)

Anyone know how to fix this issue?

Usually this error means that you are not authenticated

Hmm, how do I authenticate? In the moralis.initialize(“appid” , “”, “master key”)? This still gives the same error

you could try to use await metadataFile.saveIPFS({useMasterKey:true});

2 Likes

This worked, thanks!

how are you hiding the master Key ?

you don’t have to hide it when you run cloud functions, in front end you should not use the master key

1 Like