{"id":24781,"date":"2015-08-01T08:35:49","date_gmt":"2015-08-01T03:05:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24781"},"modified":"2015-08-03T11:12:43","modified_gmt":"2015-08-03T05:42:43","slug":"delete-file-from-amazon-s3-using-javascript-sdk","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/delete-file-from-amazon-s3-using-javascript-sdk\/","title":{"rendered":"Delete file from Amazon s3 using Javascript sdk"},"content":{"rendered":"<p>Now a days <a title=\"AWS CodeDeploy Using S3\" href=\"http:\/\/www.tothenew.com\/blog\/aws-code-deploy-using-s3\/\">AWS S3 is commonly used<\/a> as data storage. So there is one common requirement to delete a file or directory from S3. There are plenty of options avaibale to do the same, so we using javascript implementation for this. There are basic two steps by which you can delete a file from S3.<\/p>\n<p>1. Javascript sdk integration:<\/p>\n<p>[java]<br \/>\n&lt;script src=&quot;https:\/\/sdk.amazonaws.com\/js\/aws-sdk-2.1.24.min.js&quot;&gt;&lt;\/script&gt;<br \/>\n      &lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\n        AWS.config.update({<br \/>\n            accessKeyId : &#8216;YOUR_ACCESS_KEY&#8217;,<br \/>\n            secretAccessKey : &#8216;YOUR_SECRET_KEY&#8217;<br \/>\n        });<br \/>\n        AWS.config.region = &#8216;REGION&#8217;; \/\/ ex: ap-southeast-1<br \/>\n    &lt;\/script&gt;<br \/>\n[\/java]<\/p>\n<p>More on integration : <a title=\"s3 bucket javascript integration\" href=\"http:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/cli-chap-getting-started.html\"> s3 bucket javascript integration <\/a><\/p>\n<p>2. Javascript method to delete file.<\/p>\n<p>[java]<br \/>\nfunction deleteFile() {<br \/>\n    var bucketInstance = new AWS.S3();<br \/>\n    var params = {<br \/>\n        Bucket: &#8216;BUCKET_NAME&#8217;,<br \/>\n        Key: &#8216;FILENAME&#8217;<br \/>\n    };<br \/>\n    bucketInstance.deleteObject(params, function (err, data) {<br \/>\n        if (data) {<br \/>\n            console.log(&quot;File deleted successfully&quot;);<br \/>\n        }<br \/>\n        else {<br \/>\n            console.log(&quot;Check if you have sufficient permissions : &quot;+err);<br \/>\n        }<br \/>\n    });<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>You can read the full documentation here : <a title=\"s3 Javascript documentation\" href=\"http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/latest\/AWS\/S3.html#getObject-property\"> s3 Javascript methods documentation <\/a><\/p>\n<p>In AWS terminology a file is called as an Object. We just need to call the deleteObject method which takes bucketName and fileName as parameters.<\/p>\n<p>This method will delete the file from s3 and check the response to make sure the file is deleted or not.<\/p>\n<p>To use this library please make sure that you have enabled cros configuration. Also it is not advisable to display your keys directly on page, so you can use Amazon Cognito or web identity federation feature. To know more use the below given link : <a href=\"http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/guide\/browser-configuring.html\">http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/guide\/browser-configuring.html<\/a><\/p>\n<p>Hope this will help you \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now a days AWS S3 is commonly used as data storage. So there is one common requirement to delete a file or directory from S3. There are plenty of options avaibale to do the same, so we using javascript implementation for this. There are basic two steps by which you can delete a file from [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[1174,7,1],"tags":[1332,4840,2089,2090],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24781"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/203"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24781"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}