No configuration setting found for key 'conf' while trying to use ConfigFactory.parseString

1.9k views Asked by At

I am trying to read my application.conf which is stored in in my s3 bucket.I used Bufferedsource to read from s3 but when I try to use ConfigFactory.parseString(source.mkString).getConfig("conf") it did not find the 'conf' which is there.Below is my source code :

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.s3.model.S3Object
import com.amazonaws.services.s3.{AmazonS3Client, AmazonS3ClientBuilder, AmazonS3URI}
import scala.collection.JavaConversions._

import scala.io.{BufferedSource, Source}


object Test {
  def main(args: Array[String]): Unit = {


    import com.amazonaws.auth.BasicAWSCredentials
    val credentials = new BasicAWSCredentials("key", "secertkey")
  //  val credentialsProvider = new DefaultAWSCredentialsProviderChain()
    val s3Client = new AmazonS3Client(credentials)
    val uri: AmazonS3URI = new AmazonS3URI("s3://test-buck/conf/application.conf")
    val s3Object: S3Object = s3Client.getObject(uri.getBucket, uri.getKey)

    val source: BufferedSource = Source.fromInputStream(s3Object.getObjectContent)
    try {
      println(source.mkString)
      import com.typesafe.config.{Config, ConfigFactory}
      val rawConfig: Config = ConfigFactory.parseString(source.mkString)
      val rootConfig = rawConfig.getConfig("conf")

      println(rootConfig)
//      println(rotConfig)
    } finally {
      source.close()
    }

  }

}

My application config looks like below

  conf {
    
       source_data_list = ["OL", "SB","1CP"]
//some other value
    
 OL {
    filename = "receipts_delta_GBR_14_10_2017.csv"
    sftp_conf {
      hostname = "endpoint"
      port = "22"
      username = "ubuntu"
      pem = "pemfile"
      filetype = "csv"
      delimiter = "|"
      directory = "/home/ubuntu/data"
    }

  }
    }

Not sure what i am doing wrong here .Same application config if i put on resource and try loading by ConfigFactory.load("application.conf").getConfig("conf") it works as expected . Any clue on this would help .

Exception I got

Exception in thread "main" Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'conf' at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124) at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:145) at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159) at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164) at com.typesafe.config.impl.SimpleConfig.getObject(SimpleConfig.java:218) at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:224) at com.typesafe.config.impl.SimpleConfig.getConfig(SimpleConfig.java:33) at com.dsm.utils.Test$.main(Test.scala:26) at com.dsm.utils.Test.main(Test.scala)

1

There are 1 answers

0
Tomer Shetah On BEST ANSWER

Actually you succeeded to read the configuration.

The issue you're having is because of BufferedSource. The Buffered source can be read once. You read it, in order to debug, I guess, and then the source gets to the end. The second time you read it, in order to populate rawConfig you get an empty string. I solved it by extracting the configuration string into a variable, and then using it.

val config = source.mkString
println(s"config is: $config")
val rawConfig: Config = ConfigFactory.parseString(config)
val rootConfig = rawConfig.getConfig("conf")
println(s"rootConfig is: $rootConfig")

The output is:

rootConfig is: Config(SimpleConfigObject({"OL":{"filename":"receipts_delta_GBR_14_10_2017.csv","sftp_conf":{"delimiter":"|","directory":"/home/ubuntu/data","filetype":"csv","hostname":"endpoint","pem":"pemfile","port":"22","username":"ubuntu"}},"source_data_list":["OL","SB","1CP"]}))