Chaitanya
Chaitanya

Reputation: 3638

Download pdf file from s3 using akka-stream-alpakka

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 ?

Upvotes: 0

Views: 550

Answers (2)

YouXiang-Wang
YouXiang-Wang

Reputation: 1127

  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 {

}

Upvotes: 3

cbley
cbley

Reputation: 4608

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.")
}

Upvotes: 2

Related Questions