How to upload files in s3 bucket
Posted By : Chetan Hallan | 22-Jun-2015
Simple Storage Service s3 in short is a web services interface that can be use to store and retrieve any amount of data, at any time, from anywhere on the web.
Prerequisites:-
1. Amazon s3 account with AccessKey and SecretKey.
2. Created Bucket with public privacy settings.
To upload files in s3 from Grails Appliation can be done by following the below mentioned steps:-
1. You first need to install a grails plugin named as "aws-sdk" to do so just mention the below line in ur BuildConfig
runtime ':aws-sdk:1.9.33'
2. Now Save the changes and then right click on project and search for refresh dependencies after the completion of this your plugin is installed to use.
3. Now mention the following code to upload a file in s3. Here "CannedAccessControlList.PublicRead" will upload the file with public access rights and can be easily accessable and used publicly.
amazonWebService.s3.putObject(new PutObjectRequest('bucket-name', "folderInBucket/fileName.txt", new File("localFile.txt")).withCannedAcl(CannedAccessControlList.PublicRead))
4. After adding this you will see some errors to solve them press cntrl+shift+o and import the all used s3 packages and inject the amazonWebService where you are using the above code.
5. Now you need to enter your configurations of s3 in "Config.groovy" file which will be used by plugin to connect to your bucket and do upload.
// Amazon access details
grails {
plugin {
awssdk {
accessKey = '#MentionYourAccessKeyHere' // Default access key
secretKey = '#MentionYourSecretKeyHere' // Default secret key
}
}
}
6. Run the project and code will do its magic of uploading files in s3.
Thanks
Feel free to comment for any queries
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chetan Hallan
Chetan is a bright Full Stack Java Developer, he has expertise in JAVA, Spring, Hibernate, AngularJS and MySQL.