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, get the pdf source and am able to download the pdf. However, instead of downloading the file, I now need to have the pdf as an array of bytes in memory so that I can perform some other processing.

My current code looks something like this

val filePath = "certificate.pdf"
val value1: Future[IOResult] =
  awsS3Bucket
    .getSource(data.s3PdfPath)
    .toMat(FileIO.toPath(Paths.get(filePath)))(Keep.right)
    .run()

I tried a few combinations, but was unsuccessful. Can someone please point out what change needs to be made in the above code so as to have type of value1 an Future[Array[Bytes]] instead of Future[IOResult].

TIA.

Chaitanya
  • 3,590
  • 14
  • 33

1 Answers1

1

I think IOResult is only the status of download:

final case class IOResult(count: Long, status: Try[Done])

So if you want to get Array[Bytes], you need to load the pdf in memory after the download is completed.

value1.onComplete {
  case Success(_) => {
    // load pfile into MEM here
  }
  case Failure(e) => {
    e.printStackTrace()

  }
}
YouXiang-Wang
  • 1,119
  • 6
  • 15
  • Loading in memory the whole file is totally defeating the streaming – cchantep Feb 06 '20 at 19:30
  • @cchantep I totally understand your point, but my requirement is such that I need to download the file as an Array[Byte] and then send this file as an attachment in an email. The email client does not accept a streaming api and is based on the traditional blocking call. Hence I was left with no choice but to download this file as an array of bytes and then add it as an attachment in the email. – Chaitanya Feb 06 '20 at 20:46