[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

I also get the same error:
Here’s my code:

    const image = "data:image/png;base64," + btoa(svgData);

    Moralis.initialize(
        MORALIS_APP_ID, 
        "",
        MORALIS_MASTER_KEY
    );
    const file = new Moralis.File(
        "smolrunners_" + String(supply) + ".svg", 
        {base64 : image }
    );
    await file.saveIPFS({useMasterKey:true});

Can anyone tell me how to fix this issue?

i used it then also it is showing me same error

did you try with await file.saveIPFS(null, {useMasterKey:true}); ?

yes i did.
can i show my code so you can help me

you can use uploadFolder from web3api directly too in order to upload to IPFS

const Moralis = require(“moralis/node”);
const express = require(‘express’);
const multer = require(‘multer’);
let storage = multer.memoryStorage()
const upload = multer({storage: storage , dest: ‘uploads/’});

const router = express.Router();

const serverUrl = “…”;

const appId = “…”;

const masterkey = “…”

Moralis.start({ serverUrl, appId, masterkey});

router.post(’/get_and_convert’,upload.single(‘file’), async (req,res) => {

const imagestring = await uploadimage(req.file);

console.log(imagestring);

const imagefiles = new transaction({

     image : imagestring,

})

 

try {

    const dataToSave = await imagefiles.save();

    res.status(200).json(dataToSave)

}

catch (error) {

    res.status(400).json({ message: error.message })

}

})

uploadimage = async(fileobj) => {

const fileType = fileobj.mimetype;

const value = fileType.substring(fileType.lastIndexOf(`/`) + 1);

const imageFile = new Moralis.File(`myNFT.${value}`, {base64 : fileobj.buffer.toString("base64")});



await imageFile.saveIPFS(null,{useMasterKey: true});

imageURI = imageFile.ipfs();

console.log(imageURI);

}

what is issue because of which i am getting that error

Please let me know a early as possible

please check here on how to post code on forum:

can you please check once the code.

this works fine for me:

const Moralis = require('moralis/node')
const serverUrl = 'https://23423:2053/server'
const appId = '5234'
const masterKey = '523425'


async function x() {
  await Moralis.start({serverUrl: serverUrl, appId: appId, masterKey: masterKey})


    const imageFile = new Moralis.File("test_file", {base64 : "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3"});
    f = await imageFile.saveIPFS({useMasterKey: true});
    console.log(JSON.stringify(f))
}

x()
1 Like

But I want IPFS link so what changes are required in this

if you run that code, you can see the output and you can find out after that how to get the url for ipfs