Created
August 25, 2021 19:36
-
-
Save polluterofminds/b1af5cd419cb878637c52158c0ebccb0 to your computer and use it in GitHub Desktop.
PFP File Upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PinataJWT = "YOUR PINATA JWT"; | |
const fs = require("fs"); | |
const axios = require("axios"); | |
const FormData = require("form-data"); | |
const recursive = require("recursive-fs"); | |
const basePathConverter = require("base-path-converter"); | |
async function main() { | |
try { | |
const path = "./YOUR_FOLDER_PATH_RELATIVE_TO_PROJECT_ROOT"; | |
const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`; | |
recursive.readdirr(path, function (err, dirs, files) { | |
let data = new FormData(); | |
files.forEach((file) => { | |
data.append(`file`, fs.createReadStream(file), { | |
filepath: basePathConverter(path, file), | |
}); | |
}); | |
const metadata = JSON.stringify({ | |
name: "Pinatas", | |
}); | |
data.append("pinataMetadata", metadata); | |
return axios | |
.post(url, data, { | |
maxBodyLength: "Infinity", | |
headers: { | |
"Content-Type": `multipart/form-data; boundary=${data._boundary}`, | |
Authorization: `Bearer ${PinataJWT}`, | |
}, | |
}) | |
.then(function (response) { | |
console.log(response.data); | |
process.exit(0); | |
}) | |
.catch(function (error) { | |
throw error; | |
}); | |
}); | |
} catch (error) { | |
console.log(error); | |
process.exit(1); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment