Storing files on Amazon S3 using Scala

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.auth.BasicAWSCredentials
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.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/*"

About Neelkanth Sachdeva

Project Manager @ eCIFM Solutions Inc. Previous : Senior Software Engineer @ Evernym Inc. Previous : Software Consultant @ Knoldus Software LLP Software Consultant @ Inphina Technologies
This entry was posted in Web. Bookmark the permalink.

One Response to Storing files on Amazon S3 using Scala

  1. Pedro Mendes says:

    Hi Neelkanth,

    You can make your files public without write a policy. I could do this with the following code:

    // …. add these classes to be imported
    import{CannedAccessControlList, PutObjectRequest}

    // …. create aws credentials and s3client object
    val objectRequest:PutObjectRequest = new PutObjectRequest(“BUCKET_NAME”,”FILE_PATH”, file);

    // Instead upload using amazonS3Client.putObject(bucketName, “FileName”, fileToUpload), use just you PutObjectRequest object

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s