I'm trying to upload very large files in chunks to a server. The server side works great. In the function I've come up with, it tries to upload all the files at once. I'm new to async/await and am unsure how to proceed. The goal is to just upload one at a time in chunks.
Note that I've stripped this out of a class, updated for clarity, and remove all the unnecessary bits.
Using node-fetch to add fetch().
Thanks for any help or advice :)
let tree = []
const start = () => {
scan('/foo/bar', err => {
tree.forEach(file => {
upload(file)
})
})
}
const scan = dir => { ... } // assigns array of files to tree
const upload = file => {
const stream = fs.createReadStream(file)
stream.on('data', async chunk => {
stream.pause()
try {
let response = await fetch('https://some/endpoint', {
method: 'post',
body: JSON.stringify({
file: file,
chunk: chunk.toString('base64')
})
})
stream.resume()
} catch(e) {
// handle errors
}
})
stream.on('end', () => {
console.log('end', file)
})
stream.on('error', err => {
console.log('error', err)
})
}