How to use Amazon AWS S3 API in angular

amazon-angular

Amazon Simple Storage Service(S3) is service that provides the cloud storage simple web service interface to store and retrieve data from anywhere. Now we send 5 TB single object amount data to store in cloud through S3 web service

Amazon provide JavaScript  API to use S3 bucket-
First, we have to run bower install to download the AWS library
            Bower install AWS-SDK
Then add this lib path to our index file
         <script src="bower_components/aws-sdk/dist/aws- sdk.min.js"></script>
So now we configure S3 service credential
AWS.config.update({
        accessKeyId: 'your id',
        secretAccessKey: 'your access key'
    });
    AWS.config.region = 'your region';
    var bucket = new AWS.S3({params: {Bucket: 'your bucket name'}
});
Store object in S3 Bucket –
For store object in the bucket, we use our variable that we declare above  ‘bucket'.
But first we have to provide some data to this bucket like file name,  type etc, so we create an object for this
       var params = {
            Key: your file name,
            ContentType: fileType,
            Body: file,
            ServerSideEncryption: 'AES256'
        };
bucket.putObject(params, function (err, data) {
            if (err) {
                toastr.error(err.message, err.code);
                return false;
            } else {
                Console.log(“successfully store”,data);
            }
Delete object in S3 Bucket – 
var params = {
                Bucket: 'your bucket name',
                Key: filename that you provide while store
            };
            bucket.deleteObject(params, function (err) {
                if (err) {
                    console.log("err", err.message, err.code);
                    toastr.error(err.message, err.code);
                    return false;
                } else {
                    toastr.success("File deleted successfully");
                }
            });

 

Hope you will enjoy. Please visit  www.technoalerts.com  for more cool article.

Leave a Reply

Your email address will not be published. Required fields are marked *