Created
February 18, 2013 16:38
-
-
Save joelongstreet/4978657 to your computer and use it in GitHub Desktop.
A proof showing how to use Amazon S3 as a simple data store for image objects with authors and captions.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>AS3 Rest Proof</title> | |
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script> | |
<script> | |
var as3_path = 'http://restproof.s3.amazonaws.com/' | |
$(function(){ | |
$.ajax({ | |
type : 'GET', | |
url : as3_path, | |
dataType : 'xml', | |
success : function (data){ | |
var $xml = $(data) | |
var contents = $xml.find('Contents') | |
//The xml is returned in chronological order which | |
//might turn out to be kind of a pain | |
for(var i=0; i<contents.length; i++){ | |
var key = $(contents[i]).find('Key').text(); | |
//There's no guarantee these requests will return | |
//in the order you requeted them. Your application | |
//logic will need complexity to handle that. | |
fetch(key); | |
} | |
}, | |
error : function (err) { | |
console.log('error: ', err); | |
} | |
}); | |
}); | |
var fetch = function(path){ | |
$.ajax({ | |
type : 'GET', | |
dataType : 'text', | |
url : as3_path + encodeURIComponent(path), | |
success : function(data, status, xhr){ | |
//The Headers just come back in a fat blob, so we need to | |
// parse them to make them usable. | |
var image = as3_path + encodeURIComponent(path); | |
var headers = xhr.getAllResponseHeaders().split(/\n/g); | |
var author = ''; | |
var caption = ''; | |
for(var i=0; i<headers.length; i++){ | |
if(headers[i].indexOf('author') != -1){ | |
author = headers[i].replace('x-amz-meta-author: ', ''); | |
} else if(headers[i].indexOf('caption') != -1){ | |
caption = headers[i].replace('x-amz-meta-caption: ', ''); | |
} | |
} | |
var tmpl = "<div class='as3_object'><h2>" + author + "</h2><p>" + caption + "</p><img src='" + image + "' /></div>"; | |
$('body').append(tmpl); | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment