Amazon S3 serves the purpose of “storage for the Internet” . S3 stand for ” Simple Storage Service”. Amazon S3 can be used for storing and retrieving any amount of data at any time, from anywhere on the web. You can use simple interface “AWS Management Console” provided by Amazon for creating buckets for storage , adding files in those buckets and for many other tasks.
Here you’ll get the idea about storing the files on Amazon S3 using Scala and how we can make all items “public” within a bucket.
You have to include the dependency below for using Amazon S3.
"com.amazonaws" % "aws-java-sdk" % "1.0.002"
This code snippet would help you to create a bucket and store the items in that bucket on Amazon S3.
You’d find your “AWS Access Key” & “AWS_Security_Key” in the “Security Credential” option of your Amazon S3 account.
package tester
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.auth.BasicAWSCredentials
import java.io.File
import play.api._
object AmazonS3FileUploadExample extends App {
val bucketName = "neelkanthbucket" // specifying bucket name
//file to upload
val fileToUpload = new File("/home/neelkanth/Desktop/Neelkanth_Sachdeva.png")
/* These Keys would be available to you in "Security Credentials" of
your Amazon S3 account */
val AWS_ACCESS_KEY = "***************************"
val AWS_SECRET_KEY = "***************************"
val yourAWSCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)
val amazonS3Client = new AmazonS3Client(yourAWSCredentials)
// This will create a bucket for storage
amazonS3Client.createBucket(bucketName)
amazonS3Client.putObject(bucketName, "FileName", fileToUpload)
}
In order to make the files in a bucket Public , you have to write the policy for that bucket. This policy would make all the items publicly accessible within a bucket. You’d find the option of adding a policy in the properties option of a bucket.
{
"Version": "2008-10-17",
"Id": "Policy13415657575",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::neelkanthbucket/*"
}
]
}





