CloudFunction not saving files in IPFS

Hello,

I am trying to save a json object in the IPFS from a cloud function, but I am getting the error " Error: {ā€œmessageā€:ā€œfile.saveIPFS is not a functionā€,ā€œcodeā€:141}".
What am I doing wrong? Is it possible to do it from a cloud function?

I know that would be better to do it in the client side, but in this case I can not trust in the client, so I need to do it in the cloud function.

Moralis.Cloud.define("saveMetadataForNFT", async (request) => {
 
  var metadata = {
      "name" : request.params.name,
      "description": request.params.description,
      "location": request.params.location
   };

  const jsonStr = JSON.stringify(metadata);

  const data = Array.from(Buffer.from(jsonStr));
  const file = new Moralis.File('metadata_'+request.params.name, data);
  await file.saveIPFS();
 
  return {
  	"ipfsLocation": "test"
  };
});

Thanks,
Pedro

1 Like

Hi pedrosantos,

The good news is itā€™s not you is itā€™s us. The bad news is that saveIFPS() is not currently supported in Cloud Functions. This is probably something that should be added, but currently this functionality is only available in the SDK- i.e. from the browser or in Nodejs.

1 Like

Hello Moralis,

The saveIIPFS() function is working right now, but how can I get the hash of the file?

if I call the method named hash() over the object file, I got the error: ā€œfile.hash is not a functionā€

file.hash()

I already tried to log all the keys of the object named file, but I was not able to find any key that returns the hash.

I need it to build the IPFS url.

Thanks,
Pedro

there is a different function to save to IPFS from cloud code, if that is what you are trying to do:
https://docs.moralis.io/moralis-server/cloud-code/cloud-functions#ipfs

you can print all the object with console.log(file)

you can also try: console.log(file._ipfs)

2 Likes

It worked!! Many thanks @cryptokid