Skip to content

Instantly share code, notes, and snippets.

@saurshaz
Last active October 3, 2016 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurshaz/47c1ec31a9c8e16ca1208c6c3f91d480 to your computer and use it in GitHub Desktop.
Save saurshaz/47c1ec31a9c8e16ca1208c6c3f91d480 to your computer and use it in GitHub Desktop.
aws-uploader riot component with server side code
const riot = require('riot')
<aws-uploader>
<div class='container'>
<form method="POST" action="/save-details">
<input type="file" id="file-input" onchange={initUpload}>
<p id="status">Please select a file</p>
<section if={opts.imagefile === 'true'}>
<img style="border:1px solid gray;width:300px;" id="preview" src="/images/default.png">
<input type="hidden" id="avatar-url" name="avatar-url" value="/images/default.png">
</section>
<button>Submit</button>
</form>
</div>
<script type="es6">
let self = this
/*
Function to carry out the actual PUT request to S3 using the signed request from the app.
*/
self.uploadFile = (file, signedRequest, url) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', signedRequest);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
if(self.opts.imagefile === 'true'){
self.root.querySelector('#preview').src = url;
self.root.querySelector('#avatar-url').value = url;
}
self.parent.location_url = url
self.parent.update()
}
else{
alert('Could not upload file.');
}
}
};
xhr.send(file);
}
/*
Function to get the temporary signed request from the app.
If request successful, continue to upload the file using this signed
request.
*/
self.getSignedRequest = (file) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', `/sign-s3?file-name=${file.name}&file-type=${file.type}`);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
const response = JSON.parse(xhr.responseText);
self.uploadFile(file, response.signedRequest, response.url);
}
else{
alert('Could not get signed URL.');
}
}
};
xhr.send();
}
/*
Function called when file input updated. If there is a file selected, then
start upload procedure by asking for a signed request from the app.
*/
self.initUpload = () => {
const files = this.root.querySelector('#file-input').files;
const file = files[0];
if(file == null){
return alert('No file selected.');
}
self.getSignedRequest(file);
}
self.on('mount', ()=>{
let self = this
// self._.navTo(null,`${subview}&target=#um&fragment=${subview}`,"#um",{},"um");
})
</script>
</aws-uploader>
<!--
Step # 2
/// server side code (node.JS can be any lang)
app.get('/sign-s3', (req, res) => {
const s3 = new aws.S3();
s3.config.update({
accessKeyId: process.env.S3_KEY_ID,
secretAccessKey: process.env.S3_ACCESS_KEY_ID
});
const fileName = req.query['file-name'];
const fileType = req.query['file-type'];
const s3Params = {
Bucket: process.env.S3_BUCKET,
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if(err){
console.log(err);
return res.end();
}
const returnData = {
signedRequest: data,
url: `https://${process.env.S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
res.write(JSON.stringify(returnData));
res.end();
});
});
Step # 3
- setup a bucket on AWS and setup CORS settings for that. share credentaials of bucket in steap # 2 code
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment