AWS image to IPFS

hi, I want to ask how to pre put the image to be save not from the input type=“file” but from a link in AWS. Like this photo https://s3.ap-southeast-1.amazonaws.com/amethaverse.io/assets/images/test_1.png

nftFile.files[0] from the <input id=“nftFile” type="file>

This code works, when choosing the photo. I want the photo to be already selected.

   const nftFileMint = new Moralis.File("nft.jpg", nftFile.files[0]);
   await nftFileMint.saveIPFS();

Will this code work?

 const test =   new Moralis.File("image.png", {base64 : btoa(JSON.stringify("https://s3.ap-southeast-1.amazonaws.com/amethaverse.io/assets/images/test_1.png"))});
 await test.saveIPFS();

You could try to fetch the image first from that url, and then to convert it to base64.

how do i fetch the image? do i btoa(JSON.stringify()); it?

made this working from a cloud function:

Moralis.Cloud.define("test_fetch", async function(request){
    im1 = "https://s3.ap-southeast-1.amazonaws.com/amethaverse.io/assets/images/test_1.png"
    im1_data = await Moralis.Cloud.httpRequest({url: im1})
    test = new Moralis.File("image.png", {base64 : btoa(im1_data.buffer)});
    f = await test.save({useMasterKey: true});
    return f
});
1 Like

cool! will try this one out. Thank you as always!

hi! i’m getting

you need to add this in your cloud code too:
const btoa = function(str){ return Buffer.from(str).toString('base64'); }
I forgot that I had it there.

Moralis.Cloud.define("test_fetch", async function(request){
    im1 = "https://s3.ap-southeast-1.amazonaws.com/amethaverse.io/assets/images/test_1.png"
    im1_data = await Moralis.Cloud.httpRequest({url: im1})
    const btoa = function(str){ return Buffer.from(str).toString('base64'); }
    test = new Moralis.File("image.png", {base64 : btoa(im1_data.buffer)});
    f = await test.save({useMasterKey: true});
    return f
});

Will this work as well when i tried to put it in javascript? when i do try to put it in JS file i get this

it looks like Moralis.Cloud.httpRequest is specific to cloud functions, in local js probably you can do it with fetch to get data from an url, but sometimes you will get CORS errors when you will test locally

1 Like