Created
June 27, 2012 01:17
-
-
Save marcuswestin/3000643 to your computer and use it in GitHub Desktop.
Upload all the images of a page to s3
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
| /* | |
| # 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