Amazon Simple Storage Service (S3) is storage for the Internet. It is designed to make web-scale computing easier for developers. Amazon S3 API allows operations on different components of Amazon S3 solution such as Buckets, Objects, and the Service. Today we will integrate the Amazon S3 in our Grails Application.

 

Step 1 : CREATE ACCOUNT

First of all, you need to create an account on Amazon S3. After sign-up, you will get Access Token and Secret Key of your Account which will be used to authenticate your account and use their services! These keys should not be shared with anyone. In our application, we will add these keys in Config.Groovy

 

Step 2DEPENDENCY

Now, we need to add the dependency for Amazon S3 in BuildConfig.Groovy

 

Step 3 : CALL THE API

After we have added the dependency, we’re ready to call the API in our application.

Before calling any API, we will use the said code in every method so that we authenticate the user every time the API is called :

 

First, we need to create a bucket. To create a new bucket we will call the create bucket API :

 

Second, we will add files to this bucketName :

The fileName mentioned here is a name of the file in String format. This is the Key to all the files and will be used everywhere we need to use this file.
The file mentioned here is the file in java.io.File format. This file uploaded is secured and CANNOT be accessed publically.

To access an uploaded file publicly, we need to change the ACL (AccessControlList) to PublicRead.

 

Third, To read the file uploaded with ACL as PublicRead, we can use the following URL :

The REGION here is set to US East as default. We can change it to any other region from the bucket settings in the S3 Account.

To read the file which is secured, we need to generate a Presigned URL Request and then open the file using the URL returned :

 

Forth, To delete the file uploaded in the S3 Bucket, we can call the following API :

 

Fifth, To delete the bucket in the S3 Account, we can call the following API :

Remember fileName is the unique identifier for your every single file upload, so this needs to be stored for future reference.

I hope this blog was helpful in integrating Amazon S3 within your Grails Application.