Skip to content

Instantly share code, notes, and snippets.

@NishiGaba
Last active August 1, 2017 07:14
Show Gist options
  • Save NishiGaba/db0f2b39b2d4d0ef590036857c596c6e to your computer and use it in GitHub Desktop.
Save NishiGaba/db0f2b39b2d4d0ef590036857c596c6e to your computer and use it in GitHub Desktop.
Upload Image on Amazon S3
//Upload Image on Amazon S3 using Angular JS
$scope.loadImage = function() {
var input = document.getElementById('imgId');
var file = input.files[0];
//-------------- UPLOAD IMAGE ON AMAZON S3 -----------------//
$scope.creds = {
bucket: 'bucket',
access_key: '123XYZ',
secret_key: 'XYZ987'
}
var s3 = new AWS.S3();
// Configure The S3 Object
AWS.config.update({ accessKeyId: $scope.creds.access_key, secretAccessKey: $scope.creds.secret_key });
AWS.config.region = 'ap-south-1';
AWS.config.signatureVersion = 'v4';
var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });
if(file) {
var params = { Key: file.name, ContentType: file.type, Body: file , ACL : 'public-read'};
console.log(params);
bucket.upload(params, function(err, data) {
if(err) {
// There Was An Error With Your S3 Config
alert(err.message);
console.log(err);
return false;
}
else {
// Success!
console.log(data);
alert('Upload Done');
}
})
.on('httpUploadProgress',function(progress) {
// Log Progress Information
console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment