Gksr Gksr - 1 year ago 189
Scala Question

Read properties file located in s3 in scala

I want to read a .properties file located in AWS s3 and use the properties in scala application.

Please let me know if there are any API for this or other way to solve this.

Answer Source

You can do something like this:

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.s3.model.S3Object
import com.amazonaws.services.s3.{AmazonS3Client, AmazonS3URI}
import scala.io.{BufferedSource, Source}

val credentialsProvider = new DefaultAWSCredentialsProviderChain()
val s3Client = new AmazonS3Client(credentialsProvider)
val uri: AmazonS3URI = new AmazonS3URI(filePath)
val s3Object: S3Object = s3Client.getObject(uri.getBucket, uri.getKey)

val source: BufferedSource = Source.fromInputStream(s3Object.getObjectContent)
try {
} finally {

Once you get the source you can do anything from it. Parse it using TypeSafe Config if it follows .hocon file format or parse based on whatever format the file is.

If it follows .hocon notation, you can parse it like

import com.typesafe.config.{Config, ConfigFactory}
val rawConfig: Config = ConfigFactory.parseString(contents)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download