Saving Files

IPFS is supported out of the box when using Moralis.

You can upload files with the saveIPFS() method (max file size 64 MB)

// Save file input to IPFS
const data = fileInput.files[0]
const file = new Moralis.File(data.name, data)
await file.saveIPFS();
//console.log(file.ipfs(), file.hash())
// Save file reference to Moralis
const jobApplication = new Moralis.Object('Applications')
jobApplication.set('name', 'Satoshi')
jobApplication.set('resume', file)
await jobApplication.save()
// Retrieve file
const query = new Moralis.Query('Applications')
query.equalTo('name', 'Satoshi')
query.find().then(function ([application]) {
const ipfs = application.get('resume').ipfs()
const hash = application.get('resume').hash()
console.log('IPFS url', ipfs)
console.log('IPFS hash', hash)

The data is automatically pinned.

Saving Objects

You can also upload JSON objects directly from javascript, by saving the base64 string, Moralis will automatically create the Buffer from the base64 provided:

const object = {
"key" : "value"
const file = new Moralis.File("file.json", {base64 : btoa(JSON.stringify(object))});
await file.saveIPFS();

By uploading base64, you could also upload other base64 encoded files like images

const image = "data:image/png;base64,iVBORw0KGgoAAA...."
const file = new Moralis.File("image.png", {base64 : image });
await file.saveIPFS();