Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created November 21, 2016 17:58
Show Gist options
  • Save alanshaw/2bda630b7521c9ccdf23e064833f2b92 to your computer and use it in GitHub Desktop.
Save alanshaw/2bda630b7521c9ccdf23e064833f2b92 to your computer and use it in GitHub Desktop.
Uploadcare download a bucket
// Usage:
// PRIVATE_KEY= PUBLIC_KEY= OUTPUT_DIR='./images' node uc-download-bucket.js
const Uc = require('uploadcare')(process.env.PUBLIC_KEY, process.env.PRIVATE_KEY)
const Async = require('async')
const Fs = require('fs')
const Path = require('path')
const Request = require('request')
const outputDir = Path.resolve(process.env.OUTPUT_DIR || process.cwd())
function download (file, cb) {
const { uuid, original_filename, original_file_url } = file
console.log(`downloading ${original_filename}`)
const req = Request.get(original_file_url)
req.on('error', (err) => cb(err))
const dest = Fs.createWriteStream(Path.join(outputDir, `${uuid}_${original_filename}`))
dest.on('error', (err) => cb(err))
dest.on('finish', () => cb())
req.pipe(dest)
}
const queue = Async.queue(download, parseInt(process.env.CONCURRENCY || 5))
queue.drain = () => console.log('Done')
let page = 1
let totalPages = Infinity
const limit = 100
const iteratee = (cb) => {
Uc.files.list({ page, limit }, (err, res) => {
if (err) return cb(err)
totalPages = Math.ceil(res.total / limit)
res.results.forEach(({ original_filename }) => {
console.log(`queueing ${original_filename}`)
})
queue.push(res.results)
page++
cb()
})
}
const test = () => page < totalPages
Async.doWhilst(iteratee, test, (err) => {
if (err) throw err
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment