S3Ninja an Introduction

S3Ninja is an emulator that emulates the S3API. S3Ninja provides an environment for your local system to support integration of upload a file, just as we do on S3. Currently it supports objects methods only like GET, PUT, HEAD, DELETE.

S3Ninja can be used,

  • to upload file on our local system instead of S3
  • to write integration tests that may integrate with upload of file on S3Ninja, instead of mocking the web services

Setting up S3Ninja

Follow the following steps to setup the S3Ninja emulator,

  • Download S3Ninja latest version as ZIP file
  • Unzip it to any directory
  • Start S3Ninja by typing sirius.sh start
  • Open the url http://localhost:9444 on browser
  • Run S3 API calls on http://localhost:9444/s3  [e.g. -http://localhost:9444/s3/test-bucket ]
  • After the use, stop S3Ninja by typing sirius.sh stop

Now we get the Access Key and Secret Key from the app/application.conf file in S3Ninja unzipped folder.

Lets begin the code

Include the following dependency into the build.sbt file for Amazon services,

"com.amazonaws" % "aws-java-sdk" % "1.10.30"

And Import the following packages as well,

import com.amazonaws.ClientConfiguration
import com.amazonaws.auth.{AWSCredentials, BasicAWSCredentials}
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.transfer.TransferManager

Create the AWS credentials with the help of Access key and Secret key provided by S3Ninja,

val credentials: AWSCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)

After the credentials have been set, we can create the AmazonS3Client and set S3Ninja URL as an end point to it,

val NINJA_URL = "http://localhost:9444/s3"
val amazonS3Client: AmazonS3Client = new AmazonS3Client(credentials, new ClientConfiguration())
amazonS3Client.setEndpoint(NINJA_URL)

Now we set the client to the transfer manager,

val transferManager: TransferManager = new TransferManager(amazonS3Client)

Now we upload the file on S3Ninja as,

transferManager.upload(bucket, fileName, file)

Its done, Now you can open the URL http://localhost:9444/s3 on browser to verify the uploaded file

An example to verify to upload

1.png

The bucket demoBucket is created here,

2.png

The file ninja.jpg looks like this after upload.

I hope you enjoyed it!! Happy blogging!!

You can find complete code here

2 thoughts on “S3Ninja an Introduction

Leave a Reply

%d bloggers like this: