Skip to content

Instantly share code, notes, and snippets.

@nagataka
Created December 9, 2013 12:14
Show Gist options
  • Save nagataka/7871379 to your computer and use it in GitHub Desktop.
Save nagataka/7871379 to your computer and use it in GitHub Desktop.
Listing objects in S3 bucket by AWS SDK for javaScript using facebook as Identity Provider.
<!DOCTYPE html>
<html>
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.0-rc1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
//for Identity Federation
var appId = 'YOUR FB APP ID';
var roleArn = 'arn:aws:iam::xxxxxxxxxxxx:role/IdentityFederationRole';
var fbUserId;
var bucketName = 'BUCKET NAME';
var bucketRegion = 'ap-northeast-1';
var s3Bucket = new AWS.S3({params: {Bucket: bucketName, Region: bucketRegion}});
function listObj(){
s3Bucket.listObjects(
function(error, data){
if(error){
console.log(error);
}else{
console.log(data);
}
}
);
}
/*!
* Login to your application using Facebook.
* Uses the Facebook SDK for JavaScript available here:
* https://developers.facebook.com/docs/javascript/gettingstarted/
*/
window.fbAsyncInit = function(){
FB.init({ appId: appId });
FB.login(function(response) {
s3Bucket.config.credentials = new AWS.WebIdentityCredentials({
ProviderId: 'graph.facebook.com',
RoleArn: roleArn,
WebIdentityToken: response.authResponse.accessToken
});
fbUserId = response.authResponse.userID;
button.style.display = 'block';
});
};
// Load the Facebook SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "./js/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</head>
<body>
<button onclick="listObj()">S3 - list objects</button>
<fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment