evaporate
Version:
Javascript library for browser to S3 multipart resumable uploads for browsers and with Node FileSystem (fs) Stream Support
120 lines (101 loc) • 4.59 kB
HTML
<html>
<head>
<title>Evaporate Example</title>
<style>
</style>
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.2.43.min.js"></script>
<script language="javascript" type="text/javascript" src="../evaporate.js"></script>
</head>
<body>
<div>
<input type="file" id="files" multiple />
</div>
<script language="javascript">
var files;
function authByAwsLambda (signParams, signHeaders, stringToSign, dateString) {
return new Promise(function(resolve, reject) {
var awsLambda = new AWS.Lambda({
region: 'lambda region',
accessKeyId: 'a key that can invoke the lambda function',
secretAccessKey: 'the secret'
})
awsLambda.invoke({
FunctionName: 'arn:aws:lambda:...:function:cw-signer', // arn of your lambda function
InvocationType: 'RequestResponse',
Payload: JSON.stringify({
to_sign: stringToSign,
sign_params: signParams,
sign_headers: signHeaders
})
}, function (err, data) {
if (err) {
return reject(err);
}
resolve(JSON.parse(data.Payload));
});
});
};
Evaporate.create({
aws_key: 'your aws_key here',
bucket: 'your s3 bucket name here',
customAuthMethod: authByAwsLambda
})
.then(function (_e_) {
$('#files').change(function (evt) {
files = evt.target.files;
for (var i = 0; i < files.length; i++) {
var promise = _e_.add({
name: 'test_' + Math.floor(1000000000 * Math.random()),
file: files[i],
notSignedHeadersAtInitiate: {
'Cache-Control': 'max-age=3600'
},
xAmzHeadersAtInitiate: {
'x-amz-acl': 'public-read'
},
signParams: {
foo: 'bar',
fooFunction: function () {
return 'bar';
}
},
signHeaders: {
fooHeaderFunction: function () {
return 'bar'
},
fooHeader: 'bar'
},
beforeSigner: function (xhr) {
var requestDate = (new Date()).toISOString();
xhr.setRequestHeader('Request-Header', requestDate);
},
complete: function () {
console.log('complete................yay!');
},
progress: function (progress) {
console.log('making progress: ' + progress);
}
})
.then(function (awsKey) {
console.log(awsKey, 'complete!');
});
filePromises.push(promise);
}
});
allCompleted = Promise.all(filePromises)
.then(function () {
console.log('All files were uploaded successfully.');
}, function (reason) {
console.log('All files were not uploaded successfully:', reason);
});
$(evt.target).val('');
},
function (reason) {
console.log('Evaporate failed to initialize: ', reason)
}
);
</script>
</body>
</html>