Created
December 25, 2013 07:35
-
-
Save stomita/8121020 to your computer and use it in GitHub Desktop.
Add event handler to upload files.
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
var bucketName = 'salesforce-bucket'; | |
var bucket; | |
$(function() { | |
initAWSCredentials(); | |
initEventHandlers(); | |
updateFileList(); | |
}); | |
function initAWSCredentials() { | |
var assertion = $('#samlResponse').val(); | |
if (!assertion) { | |
throw Error("No assertion is given to this page. Please access from app menu."); | |
} | |
AWS.config.credentials = new AWS.SAMLCredentials({ | |
RoleArn: 'arn:aws:iam::119301928242:role/SalesforceUserRole', | |
PrincipalArn: 'arn:aws:iam::119301928242:saml-provider/SalesforceIdP', | |
SAMLAssertion: assertion | |
}); | |
bucket = new AWS.S3({ params: { Bucket: bucketName } }); | |
} | |
function initEventHandlers() { | |
$('#fileSelect').on('change', function() { | |
uploadFile(this.files); | |
}); | |
} | |
function updateFileList() { | |
bucket.listObjects({}, function(err, resp) { | |
if (err) { throw err; } | |
$('#fileList').empty(); | |
$.each(resp.Contents, function(i, content) { | |
$('<li>').text(content.Key).appendTo($('#fileList')); | |
}); | |
}); | |
} | |
function uploadFile(files) { | |
if (files && files.length > 0) { | |
var file = files[0]; | |
var params = { Key: file.name, ContentType: file.type, Body: file }; | |
bucket.putObject(params, function(err, resp) { | |
updateFileList(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment