Created
September 1, 2022 09:29
-
-
Save leviyehonatan/5c7538cf08035b2c8fa2f660af2a57c0 to your computer and use it in GitHub Desktop.
pin to ipfs using mypinata a complete directory
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
require("dotenv").config(); | |
const fs = require("fs"); | |
const FormData = require("form-data"); | |
var rfs = require("recursive-fs"); | |
const basePathConverter = require("base-path-converter"); | |
const got = require("got"); | |
const pinDirectoryToPinata = async (src) => { | |
const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`; | |
var status = 0; | |
try { | |
const { dirs, files } = await rfs.read(src); | |
let data = new FormData(); | |
for (const file of files) { | |
data.append(`file`, fs.createReadStream(file), { | |
filepath: basePathConverter(src, file), | |
}); | |
} | |
const response = await got(url, { | |
method: "POST", | |
headers: { | |
"Content-Type": `multipart/form-data; boundary=${data._boundary}`, | |
Authorization: `Bearer ${process.env.PINATA_JWT}`, | |
}, | |
body: data, | |
}).on("uploadProgress", (progress) => { | |
console.log(progress); | |
}); | |
console.log(JSON.parse(response.body)); | |
} catch (error) { | |
console.log(error); | |
} | |
}; | |
const args = process.argv.slice(2); | |
pinDirectoryToPinata(args[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment