0

I am trying to download pdf file from S3 using the akka-stream-alpakka connector. I have the s3 path and try to download the pdf using a wrapper method over the alpakka s3Client.

def getSource(s3Path: String): Source[ByteString, NotUsed] = {
    val (source, _) = s3Client.download(s3Bucket, s3Path)
    source
  }

From my main code, I call the above method and try to convert it to a file

               val file = new File("certificate.pdf")
               val res: Future[IOResult] = getSource(data.s3PdfPath)
                                            .runWith(FileIO.toFile(file))

However, instead of it getting converted to a file, I am stuck with a type of IOResult. Can someone please guide as to where I am going wrong regarding this ?

Chaitanya
  • 3,590
  • 14
  • 33
  • You can just inspect the IOResult, and if it was successfull you can be sure that your file was written. At which point you can open it or do whatever you want with it. – cbley Feb 03 '20 at 15:10
  • @cbley I want to download the file and not just see if it was successful. I did not find any method in IOResult that has the content from the file – Chaitanya Feb 03 '20 at 15:13
  • Well, the content of the file is in the file, on disk... – cbley Feb 03 '20 at 15:14
  • @ChaitanyaWaikar - `IOResult` is general type to describe result of operation: see `status` field which is type of `Try[Done]` and describe whether operation success or failed. After future complete - you can access it content where you pointed it to store. – Ivan Kurchenko Feb 03 '20 at 15:16

2 Answers2

3
  def download(bucket: String, bucketKey: String, filePath: String) = {
    val (s3Source: Source[ByteString, _], _) = s3Client.download(bucket, bucketKey)
    val result = s3Source.toMat(FileIO.toPath(Paths.get(filePath)))(Keep.right)
      .run()

    result
  }


download(s3Bucket, key, newSigFilepath).onComplete {

}
YouXiang-Wang
  • 1,119
  • 6
  • 15
2

Inspect the IOResult, and if successful you can use your file:

res.foreach { 
  case IOResult(bytes, Success(_)) => 
    println(s"$bytes bytes written to $file")

    ... // do whatever you want with your file

  case _ =>
    println("some error occurred.")
}
cbley
  • 4,538
  • 1
  • 17
  • 32