Gksr
Gksr

Reputation: 23

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.

Upvotes: 1

Views: 3584

Answers (1)

Aravindh S
Aravindh S

Reputation: 1245

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 {
  source.mkString
} finally {
  source.close()
}

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)

Upvotes: 4

Related Questions