Created
June 2, 2022 14:04
-
-
Save polluterofminds/dcf239c8037fb5f65e0b8f6f345e8dd1 to your computer and use it in GitHub Desktop.
Large Folder Upload Submarine
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 fs = require("fs"); | |
const FormData = require("form-data"); | |
const rfs = require("recursive-fs"); | |
const basePathConverter = require("base-path-converter"); | |
const got = require('got'); | |
const submarineDirectory = async () => { | |
const url = `https://managed.mypinata.cloud/api/v1/content`; | |
const src = "RELATIVE PATH TO DIRECTORY TO UPLOAD"; | |
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}`, | |
"x-api-key": "SUBMARINE KEY" | |
}, | |
body: data | |
}) | |
.on('uploadProgress', progress => { | |
console.log(progress); | |
}); | |
console.log(JSON.parse(response.body)); | |
} catch (error) { | |
console.log(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment