{"id":18820,"date":"2015-04-10T00:03:48","date_gmt":"2015-04-09T18:33:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18820"},"modified":"2015-08-03T01:27:27","modified_gmt":"2015-08-02T19:57:27","slug":"file-upload-on-amazon-s3-server-via-rest-api-call","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/file-upload-on-amazon-s3-server-via-rest-api-call\/","title":{"rendered":"File Upload on Amazon S3 server via REST API call"},"content":{"rendered":"<p><strong>We Can upload file on Amazon S3 Server directly without intervention of web server by using REST API\u00a0call on S3 Server.<\/strong><\/p>\n<p><strong>Use Case<\/strong> : File upload was required on mobile app without integrating SDK because integrating SDK causes app to be heavier in size and also if it goes through our web\u00a0server than it would be unnecessary consumption of resources . So for that we directly made a REST api call to <a href=\"http:\/\/www.tothenew.com\/devops-aws\">Amazon S3 server<\/a>.<\/p>\n<p><strong>Following are the Required Inputs :<\/strong><\/p>\n<ul>\n<li>Date in a specific format RFC 2822.<\/li>\n<li>Bucket name which is already created on S3.<\/li>\n<li>File which needs to be uploaded.<\/li>\n<li>ContentType is the type of file.<\/li>\n<li>Access Key of S3 server.<\/li>\n<li>Secret key of S3 Server.<\/li>\n<li>Base64 Encoded Signature by using S3 secret key .First we will create signature string using content-type, date and resource.This string will be base 64 encoded using S3 secret key which will be our signature.<\/li>\n<\/ul>\n<p><strong>Code written in groovy for REST API\u00a0call :<\/strong><br \/>\n[groovy]<br \/>\nimport sun.misc.BASE64Encoder<br \/>\nimport javax.crypto.Mac<br \/>\nimport javax.crypto.spec.SecretKeySpec<\/p>\n<p>Long BUFFER_SIZE = 4096;<br \/>\nString method = &quot;PUT&quot;;<br \/>\nString bucket = &quot;BUCKET_OF_S3_SERVER&quot;<br \/>\nString secretKey = &quot;SECRET_KEY_OF_S3_SERVER&quot;<br \/>\nString filePath = &quot;FILE_NAME&quot;<br \/>\nSimpleDateFormat df = new SimpleDateFormat(&quot;EEE&#8217;, &#8216;dd&#8217; &#8216;MMM&#8217; &#8216;yyyy&#8217; &#8216;HH:mm:ss&#8217; &#8216;Z&quot;, Locale.US);<br \/>\nDate date = new Date()<br \/>\nString formattedDate = df.format(date)<br \/>\nFile uploadFile = new File(filePath);<br \/>\nif (!(uploadFile.isFile() &amp;amp;&amp;amp; uploadFile.exists())) {<br \/>\nprintln &#8216;File Not Found !!!!&#8217;<br \/>\nreturn;<br \/>\n}<\/p>\n<p>URL url = new URL(&quot;http:\/\/${bucket}.s3.amazonaws.com\/&quot; + uploadFile.name);<br \/>\nHttpURLConnection httpConn = (HttpURLConnection) url.openConnection();<br \/>\nString resource = &quot;\/&quot; + bucket + &quot;\/&quot; + uploadFile.name<br \/>\nString contentType = &quot;application\/octet-stream&quot;<br \/>\nString signn = method + &quot;\\n\\n&quot; + contentType + &quot;\\n&quot; + formattedDate + &quot;\\n&quot; + resource<br \/>\nMac hmac = Mac.getInstance(&quot;HmacSHA1&quot;);<br \/>\nhmac.init(new SecretKeySpec(<br \/>\nsecretKey.getBytes(&quot;UTF-8&quot;), &quot;HmacSHA1&quot;));<br \/>\nString signature = (new BASE64Encoder()).encode(<br \/>\nhmac.doFinal(signn.getBytes(&quot;UTF-8&quot;)))<br \/>\n.replaceAll(&quot;\\n&quot;, &quot;&quot;);<\/p>\n<p>String authAWS = &quot;AWS &quot; + &quot;ACCESS_KEY_OF_S3_SERVER&quot; + &quot;:&quot; + signature<br \/>\nhttpConn.setDoOutput(true);<br \/>\nhttpConn.setRequestMethod(method);<br \/>\nhttpConn.setRequestProperty(&quot;Accept&quot;, &quot;*\/*&quot;);<br \/>\nhttpConn.setRequestProperty(&quot;Date&quot;, formattedDate);<br \/>\nhttpConn.setRequestProperty(&quot;Content-type&quot;, contentType);<br \/>\nhttpConn.setRequestProperty(&quot;Authorization&quot;,authAWS);<br \/>\nOutputStream outputStream = httpConn.getOutputStream();<br \/>\nFileInputStream inputStream = new FileInputStream(uploadFile);<br \/>\nbyte[] buffer = new byte[BUFFER_SIZE];<br \/>\nint bytesRead = -1;<br \/>\nwhile ((bytesRead = inputStream.read(buffer)) != -1) {<br \/>\noutputStream.write(buffer, 0, bytesRead);<br \/>\n}<br \/>\noutputStream.close();<br \/>\ninputStream.close();<br \/>\nprintln &quot;Response message : &quot;+httpConn.getResponseMessage();<br \/>\n[\/groovy]<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We Can upload file on Amazon S3 Server directly without intervention of web server by using REST API\u00a0call on S3 Server. Use Case : File upload was required on mobile app without integrating SDK because integrating SDK causes app to be heavier in size and also if it goes through our web\u00a0server than it would [&hellip;]<\/p>\n","protected":false},"author":180,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":57},"categories":[518,1174,7,1400,446],"tags":[1740,1739],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18820"}],"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\/180"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18820"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18820\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}