Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created June 27, 2012 01:17
Show Gist options
  • Select an option

  • Save marcuswestin/3000643 to your computer and use it in GitHub Desktop.

Select an option

Save marcuswestin/3000643 to your computer and use it in GitHub Desktop.
Upload all the images of a page to s3
/*
# First, install node and:
sudo npm install jsdom
sudo npm install aws2js
sudo npm install request
*/
var aws = {
key: 'AWS KEY',
secret: 'AWS SECRET',
bucket: 'AWS BUCKET'
}
var base = 'http://www.your-domain.com'
var page = '/path/to/page'
var fs = require('fs')
var jsdom = require('jsdom')
var s3 = require('aws2js').load('s3', aws.key, aws.secret)
var request = require('request')
s3.setBucket(aws.bucket)
jsdom.env({
html: base + page,
scripts: ['http://code.jquery.com/jquery-1.5.min.js'],
done: function(errors, window) {
var $ = window.$;
var urls = []
$('img').each(function() {
var url = this.src
if (url[0] == '/') {
url = base + url
}
urls.push(url)
})
function next() {
if (!urls.length) {
console.log("DONE")
process.exit()
}
var url = urls.pop()
var path = url.replace(/.*\.com\//, '')
var name = encodeURIComponent(url)
request({ url:url, encoding:null }, function(err, res, buf) {
var extension = url.match(/.*\.(\w+)$/)
console.log("HERE", path, buf.length, extension[1])
s3.putBuffer(path, buf, 'public-read', { 'Content-Type':'image/'+extension[1] }, function(err, res) {
if (err) { throw err }
next()
})
})
}
next()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment