{"id":21083,"date":"2015-06-17T11:45:33","date_gmt":"2015-06-17T06:15:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=21083"},"modified":"2016-12-20T16:07:50","modified_gmt":"2016-12-20T10:37:50","slug":"aws-s3-file-upload-with-progress-bar-using-javascript-sdk","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-s3-file-upload-with-progress-bar-using-javascript-sdk\/","title":{"rendered":"AWS S3 file upload with progress bar using javascript sdk"},"content":{"rendered":"<p>You can upload files on AWS S3 using a\u00a0server side solution, but in case of larger files it is advisable to use a\u00a0client side solution. You can probably\u00a0use\u00a0<a title=\"React JavaScript Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">JavaScript<\/a> file upload feature of AWS S3.\u00a0This is simple three step feature as described below:<\/p>\n<p>Step 1 : In the head section of your page include javascript sdk and specify your keys like this:<\/p>\n<p>[code]<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;ACCESS_KEY&#8217;,<br \/>\n        secretAccessKey : &#8216;SECRET_KEY&#8217;<br \/>\n    });<br \/>\n    AWS.config.region = &#8216;AWS_REGION&#8217;;<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>Step 2\u00a0: Now create a simple html form with a file input.<\/p>\n<p>[code]<br \/>\n&lt;form id=&quot;fileUploadForm&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot;&gt;<br \/>\n&lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot; value=&quot;dataFile&quot; required=&quot;&quot;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n[\/code]<\/p>\n<p>Step 3\u00a0: Now upload your\u00a0input file to S3<\/p>\n<p>[code]<br \/>\n$(&quot;#fileUploadForm&quot;).submit(function() {<br \/>\nvar bucket = new AWS.S3({params: {Bucket: &#8216;BUCKET_NAME&#8217;}});<br \/>\nvar fileChooser = document.getElementById(&#8216;file&#8217;);<br \/>\nvar file = fileChooser.files[0];<br \/>\nif (file) {<br \/>\nvar params = {Key: &#8216;FILE_NAME&#8217;, ContentType: file.type, Body: file};<br \/>\nbucket.upload(params).on(&#8216;httpUploadProgress&#8217;, function(evt) {<br \/>\nconsole.log(&quot;Uploaded :: &quot; + parseInt((evt.loaded * 100) \/ evt.total)+&#8217;%&#8217;);<br \/>\n}).send(function(err, data) {<br \/>\nalert(&quot;File uploaded successfully.&quot;);<br \/>\n});<br \/>\n}<br \/>\nreturn false;<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>To upload the file successfully, you need to enable CORS configuration on S3.<\/p>\n<p>Generally, it is not advisable to display your keys directly on page, so you can use Amazon Cognito or web identity federation feature.<\/p>\n<p><a href=\"http:\/\/docs.aws.amazon.com\/AWSJavaScriptSDK\/guide\/browser-configuring.html\" target=\"_blank\">Know more\u00a0<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can upload files on AWS S3 using a\u00a0server side solution, but in case of larger files it is advisable to use a\u00a0client side solution. You can probably\u00a0use\u00a0JavaScript file upload feature of AWS S3.\u00a0This is simple three step feature as described below: Step 1 : In the head section of your page include javascript sdk [&hellip;]<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":69},"categories":[1174,7,1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21083"}],"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\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=21083"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/21083\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=21083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=21083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=21083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}