{"id":19092,"date":"2015-04-20T23:11:33","date_gmt":"2015-04-20T17:41:33","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19092"},"modified":"2015-05-07T17:40:08","modified_gmt":"2015-05-07T12:10:08","slug":"file-upload-on-amazon-s3-server-via-html-form","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/file-upload-on-amazon-s3-server-via-html-form\/","title":{"rendered":"File Upload on Amazon S3 server via HTML Form"},"content":{"rendered":"<p style=\"text-align: justify\"><strong>Use Case<\/strong> : \u00a0<span style=\"color: #999999\">\u00a0<span style=\"color: #003300\">File upload was require on web by end user which needs to be frequent and multiple at a time and if it goes through our tomcat server than it would be\u00a0overhead on server\u00a0. So for that we directly send the file to S3 server.<\/span><\/span><\/p>\n<p>We can upload file on Amazon S3 Server directly without routing the file through web\u00a0server by submitting\u00a0HTML form directly to S3 server with some configurations.<\/p>\n<h3><strong><br \/>\nFollowing are the Required Inputs<\/strong>:<\/h3>\n<p><\/br><\/p>\n<ul>\n<li><span style=\"color: #333333\">Bucket name which is already created on S3.<\/span><\/li>\n<li><span style=\"color: #333333\">File which needs to be uploaded.<\/span><\/li>\n<li><span style=\"color: #333333\">ContentType is the type of file.<\/span><\/li>\n<li><span style=\"color: #333333\">Access Key of S3 server.<\/span><\/li>\n<li><span style=\"color: #333333\">Secret key of S3 Server.<\/span><\/li>\n<li><span style=\"color: #333333\">Base64 Encoded Signature by using S3 secret key .<\/span><\/li>\n<li><span style=\"color: #333333\">Expiration date in ISO 8601 UTC\u00a0format.<\/span><\/li>\n<\/ul>\n<p><\/br><br \/>\nA policy needs to be created which will have required conditions on the html form. There will be expiration date after which the upload form will no longer work.\u00a0HTML form must follow these\u00a0conditions to be submitted and also the elements of HTML form must be in a specific order.<br \/>\nFirst we will create signature string using policy.<br \/>\nThis string will be base 64 encoded using S3 secret key.<\/p>\n<h3><strong><br \/>\nServer side code:<\/strong><\/h3>\n<p><\/br><\/p>\n<div style=\"background: #E6E6FA\">\n<p style=\"padding-left: 60px\"><span style=\"color: #800000\">import sun.misc.BASE64Encoder<\/span><br \/>\n<span style=\"color: #800000\"> import javax.crypto.Mac<\/span><br \/>\n<span style=\"color: #800000\"> import javax.crypto.spec.SecretKeySpec<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"color: #800000\">String aws_bucket = &#8220;S3_BUCKET_NAME&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> String aws_folderName = &#8220;ANY_FOLDER_NAME&#8221; \u00a0 \u00a0 \u00a0 \u00a0 \/\/ in which the file will be uploaded<\/span><br \/>\n<span style=\"color: #800000\"> String policy_document = &#8220;{ \\&#8221;expiration\\&#8221;: \\&#8221;EXPIRATION_DATE_IN_GMT_FORMAT\\&#8221;,&#8221; + \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ 20016-12-01T12:00:00.000Z &#8212; date example in GMT<\/span><br \/>\n<span style=\"color: #800000\"> &#8221; \\&#8221;conditions\\&#8221;: [&#8221; +<\/span><br \/>\n<span style=\"color: #800000\"> &#8220;{\\&#8221;acl\\&#8221;: \\&#8221;public-read\\&#8221; },&#8221; + \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/access control policy to apply to the uploaded file<\/span><br \/>\n<span style=\"color: #800000\"> &#8220;[\\&#8221;starts-with\\&#8221;, \\&#8221;\\$Content-Type\\&#8221;, \\&#8221;multipart\/form-data\\&#8221;],&#8221; + \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ starts-with : checks that\u00a0value begins with a given string<\/span><br \/>\n<span style=\"color: #800000\"> &#8221; {\\&#8221;bucket\\&#8221;: \\&#8221;${aws_bucket}\\&#8221;},&#8221; +<\/span><br \/>\n<span style=\"color: #800000\"> &#8220;[\\&#8221;starts-with\\&#8221;, \\&#8221;\\$key\\&#8221;, \\&#8221;${aws_folderName}\/\\&#8221;]&#8221; +<\/span><br \/>\n<span style=\"color: #800000\"> &#8221; ]&#8221; +<\/span><br \/>\n<span style=\"color: #800000\"> &#8220;}&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> String aws_secret_key = &#8220;S3_SECRET_KEY&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> String policy = (new BASE64Encoder()).encode(<\/span><br \/>\n<span style=\"color: #800000\"> policy_document.getBytes(&#8220;UTF-8&#8221;)).replaceAll(&#8220;\\n&#8221;, &#8220;&#8221;).replaceAll(&#8220;\\r&#8221;, &#8220;&#8221;);<\/span><\/p>\n<p style=\"padding-left: 60px\"><span style=\"color: #800000\">Mac hmac = Mac.getInstance(&#8220;HmacSHA1&#8221;);<\/span><br \/>\n<span style=\"color: #800000\"> hmac.init(new SecretKeySpec(<\/span><br \/>\n<span style=\"color: #800000\"> aws_secret_key.getBytes(&#8220;UTF-8&#8221;), &#8220;HmacSHA1&#8221;));<\/span><br \/>\n<span style=\"color: #800000\"> String signature = (new BASE64Encoder()).encode(<\/span><br \/>\n<span style=\"color: #800000\"> hmac.doFinal(policy.getBytes(&#8220;UTF-8&#8221;)))<\/span><br \/>\n<span style=\"color: #800000\"> .replaceAll(&#8220;\\n&#8221;, &#8220;&#8221;);<\/span><br \/>\n<span style=\"color: #800000\"> String serverUrl = &#8220;http:\/\/${aws_bucket}.s3.amazonaws.com\/&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> String accessKey = &#8220;S3_ACCESS_KEY&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> String filePath = aws_folderName+&#8221;SOME_STRING_WHICH_WILL_BE_APPENDED_TO_FILE&#8221; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\u00a0this will be the path on S3 where file will be uploaded<\/span><\/p>\n<\/div>\n<h3><strong><br \/>\nHTML form with GSP:<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Following form must be following all the conditions of policy document.<br \/>\nFor our example : Key must be starting with &#8220;aws_folderName&#8221; , acl must be same , content type must starts with &#8220;multipart\/form-data&#8221;.<\/p>\n<blockquote style=\"background: #E6E6FA\">\n<p style=\"padding-left: 60px\"><span style=\"color: #800000\"><br \/>\n&lt;form action=&#8221;${serverUrl}&#8221; name=&#8221;fileUploadForm&#8221; id=&#8221;fileUploadForm&#8221; method=&#8221;post&#8221;<\/span><br \/>\n<span style=\"color: #800000\"> enctype=&#8221;multipart\/form-data&#8221;&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;key&#8221; value=&#8221;${filePath+FILE_NAME}&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;acl&#8221; value=&#8221;public-read&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;Content-Type&#8221; value=&#8221;multipart\/form-data&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;AWSAccessKeyId&#8221; value=&#8221;${accessKey}&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;Policy&#8221; value=&#8221;${policy}&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;hidden&#8221; name=&#8221;Signature&#8221; value=&#8221;${signature}&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;input type=&#8221;file&#8221; name=&#8221;dataFile&#8221; id=&#8221;dataFile&#8221;\/&gt;<\/span><br \/>\n<span style=\"color: #800000\"> &lt;\/form&gt;<\/span><\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<p><\/br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Case : \u00a0\u00a0File upload was require on web by end user which needs to be frequent and multiple at a time and if it goes through our tomcat server than it would be\u00a0overhead on server\u00a0. So for that we directly send the file to S3 server. We can upload file on Amazon S3 Server [&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":6},"categories":[1174,7,446],"tags":[1763,1762,1764],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19092"}],"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=19092"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19092\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}