Last active
August 29, 2015 14:17
-
-
Save cryptixcoder/5e3c6258e258d4b251b6 to your computer and use it in GitHub Desktop.
Direct to S3 Upload (Single File)
This file contains hidden or 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
$('.attach').change(function(e){ | |
$('.upload-spinner').show(); | |
AWS.config.update({ accessKeyId: AWSKEY, secretAccessKey: AWSSECRET }); | |
AWS.config.region = 'us-east-1'; | |
var bucket = new AWS.S3({ params: { Bucket: AWSBUCKET } }); | |
var $file = e.target.files[0]; | |
var params = { | |
Key: $file.name, | |
ContentType: $file.type, | |
Body: $file, | |
ServerSideEncryption: 'AES256' | |
}; | |
if($file){ | |
bucket.putObject(params, function(err, response){ | |
if(err){ | |
alert(err.message); | |
return false; | |
} | |
//Successful upload | |
var filepath = BUCKETURL + $file.name; | |
$('.upload-spinner').fadeOut(); | |
$('.attached-image').val(filepath); | |
//File Preview | |
$('.preview-image').attr('src', filepath); | |
$('.preview-image').fadeIn(); | |
}).on('httpUploadProgress',function(progress) { | |
console.log(Math.round(progress.loaded / progress.total * 100) + '% done'); | |
}); | |
} | |
else{ | |
alert('No File Selected'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment