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
1 2 |
accessKey = "{{ACCESS_TOKEN}}" secretKey = "{{SECRET_KEY}}" |
Step 2 : DEPENDENCY
Now, we need to add the dependency for Amazon S3 in BuildConfig.Groovy
1 2 3 4 5 |
dependencies { ... compile 'com.amazonaws:aws-java-sdk-s3:1.11.349' ... } |
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 :
1 2 3 4 5 6 7 |
BasicAWSCredentials awsCredentials = new BasicAWSCredentials( "${grailsApplication.config.accessKey}", "${grailsApplication.config.secretKey}") AmazonS3 s3 = AmazonS3ClientBuilder.standard(). withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) .withRegion("${grailsApplication.config.region}") .build() |
First, we need to create a bucket. To create a new bucket we will call the create bucket API :
1 2 |
String bucketName = '{{BUCKET_NAME}}' //Save this for future reference. s3.createBucket(new CreateBucketRequest(bucketName)); |
Second, we will add files to this bucketName :
1 2 3 |
String fileName = '{{FILE_NAME}}' //Save this for future reference. File file = {{FILE}} s3.putObject(bucketName, fileName, file) |
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.
1 2 |
s3.putObject(new PutObjectRequest(bucketName, fileName, file) .withCannedAcl(CannedAccessControlList.PublicRead)) |
Third, To read the file uploaded with ACL as PublicRead, we can use the following URL :
1 |
https://s3-{{REGION}}.amazonaws.com/bucketName/fileName |
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 :
1 2 |
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, fileName) String presignedURL = s3.generatePresignedUrl(request) |
Forth, To delete the file uploaded in the S3 Bucket, we can call the following API :
1 |
s3.deleteObject(new DeleteObjectRequest(bucketName, keyName)) |
Fifth, To delete the bucket in the S3 Account, we can call the following API :
1 |
s3.deleteBucket(bucketName) |
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.
Recent Comments