Replies: 2 comments 4 replies
-
Where would you store your images? In any case, I would just use https://www.npmjs.com/package/fastq and with a worker to download and save them one by one. I would not process more than 100 in parallel due to file descriptors limitation on the OS, but that number could be easily increased. |
Beta Was this translation helpful? Give feedback.
4 replies
-
import { stream } from 'undici'
import { createWriteStream } from 'fs'
function downloadFactory ({ statusCode, opaque }) {
if (statusCode !== 200) {
throw new Error(`failed to download, ${statusCode}`)
}
return createWriteStream(opaque.target)
}
const urls = [
{ source: 'https://www.example.com/foo.png', target: 'images/foo.png' },
{ source: 'https://www.example.com/bar.png', target: 'images/bar.png' },
{ source: 'https://www.example.com/baz.png', target: 'images/baz.png' },
// ... many more
]
await Promise.allSettled(
urls.map(({ source, target }) => stream(source, { opaque: { target } }, downloadFactory))
)
console.log('all images downloaded') @mcollina I came up with this. Is this the correct way to download files? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a large json file which contains urls to images which i need to download to my machine.
I parse the json file and now i have an array of 10'000+ urls of images. Now my question is how can i efficiently download every image using
undici
to my machine. Preferably i want to download them in parallel so its fast but also throttle the request rate so i don't get rate-limited?Beta Was this translation helpful? Give feedback.
All reactions