{"id":24872,"date":"2015-08-04T11:10:59","date_gmt":"2015-08-04T05:40:59","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24872"},"modified":"2015-08-04T11:38:34","modified_gmt":"2015-08-04T06:08:34","slug":"integrating-amazon-s3-in-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-amazon-s3-in-grails-application\/","title":{"rendered":"Integrating Amazon S3 in Grails Application"},"content":{"rendered":"<p>Recently in my project, we needed to store documents on some cloud based storage. So <a title=\"Top 10 Benefits Of Using AWS\" href=\"http:\/\/www.tothenew.com\/blog\/top-10-benefits-of-using-aws\/\">we choose Amazon S3<\/a> as it is secure and has proper backup mechanism and enough data centres so that we have minimum latency to access the server. Amazon has provided a great API available in different languages and we can use their Java API in our <a title=\"Grails Application Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails application<\/a><\/p>\n<p>In this blog we will see how to integrate the Amazon JAR in your Grails application and use different operations like upload\/download\/delete file to\/from S3 in some simple steps.<\/p>\n<p><strong><em>Step 1<\/em><\/strong> : First you need to have an account on Amazon so that you can consume their services. You will get access_token and secret_key of your owning account which is used for authentication to consume Amazon services through any API call. These credentials should be very secure and should not be exposed to end user. Save credentials into some file let&#8217;s say &#8220;AWSCredentials.properties&#8221; and file content should be something like :<\/p>\n<p>[groovy]<br \/>\n\/\/ File: AWSCredentials.properties<br \/>\naccessKey=abcdefghij1234012345678<br \/>\nsecretKey=8667f7f66f6fdf+F\/jfj987389784<br \/>\n[\/groovy]<\/p>\n<p><strong><em>Step 2\u00a0<\/em><\/strong>: Add a line in the &#8220;dependency&#8221; block for fetching the AWS SDK<\/p>\n<p>[groovy]<\/p>\n<p>dependencies {<br \/>\n\t&#8230;<br \/>\n\tcompile &#8216;com.amazonaws:aws-java-sdk-s3:1.10.8&#8217;<br \/>\n\t&#8230;<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p><strong><em>Step 3\u00a0<\/em><\/strong>: After you&#8217;ve added a dependency, create an &#8220;S3Client&#8221; and authenticate it with given credentials :<\/p>\n<p>[groovy]<br \/>\nFile file = grailsApplication.mainContext.getResource(&#8216;WEB-INF\/AWSCredential.properties&#8217;).file<br \/>\nFileInputStream fileInputStream = new FileInputStream(file)<br \/>\nAmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(fileInputStream))<br \/>\n[\/groovy]<\/p>\n<p>&#8230; or register a bean named &#8220;s3Client&#8221; usable throughout the application as follows:<\/p>\n<p>[groovy]<\/p>\n<p>\/\/ resources.groovy<br \/>\nFile file = grailsApplication.mainContext.getResource(&#8216;WEB-INF\/AWSCredentials.properties&#8217;).file<br \/>\nFileInputStream fileInputStream = new FileInputStream(file)<\/p>\n<p>s3Client(AmazonS3Client, new PropertiesCredentials(fileInputStream)) {}<\/p>\n<p>[\/groovy]<\/p>\n<p>So far so good. Once we&#8217;ve created a new &#8220;S3Client&#8221;, its simply a matter of calling the requisite API methods to perform the respective actions. Let&#8217;s try out a few operations.<\/p>\n<h3>Create a new bucket:<\/h3>\n<p>[groovy]<br \/>\ns3Client.createBucket(new CreateBucketRequest(&#8216;My-Bucket&#8217;));<br \/>\n[\/groovy]<\/p>\n<h3>Upload a new file on a given bucket:<\/h3>\n<p>[groovy]<br \/>\nString key = UUID.randomUUID().toString()  \/\/ Key should be unique for each upload.<br \/>\nFile file = new File(&#8216;MyPath&#8217;)               \/\/ You can upload any type of file.<br \/>\nPutObjectRequest putObjectRequest = new PutObjectRequest(&#8216;My-Bucket&#8217;, key, file)<br \/>\nputObjectRequest.withCannedAcl(CannedAccessControlList.AuthenticatedRead)) \/\/ secure everything<br \/>\ns3Client.putObject(putObjectRequest)<br \/>\n[\/groovy]<\/p>\n<h3>Fetch a file from a bucket:<\/h3>\n<p>[groovy]<br \/>\nGetObjectRequest getObjectRequest = new GetObjectRequest(&#8216;My-Bucket&#8217;, key)<br \/>\ns3Client().getObject(getObjectRequest)<br \/>\n[\/groovy]<\/p>\n<h3>Delete a file from a bucket<\/h3>\n<p>[groovy]<br \/>\nDeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(&#8216;My-Bucket&#8217;, key)<br \/>\ns3Client().deleteObject(deleteObjectRequest);<br \/>\n[\/groovy]<\/p>\n<p>Remember &#8220;key&#8221; is the unique identifier for your every single file upload , so this needs to be stored for future reference.<\/p>\n<p>I hope this blog was helpfull to integrate Amazon S3 within your grails application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in my project, we needed to store documents on some cloud based storage. So we choose Amazon S3 as it is secure and has proper backup mechanism and enough data centres so that we have minimum latency to access the server. Amazon has provided a great API available in different languages and we can [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[1174,7,446],"tags":[2117,2116],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24872"}],"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\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24872"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24872\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}