Chaitanya
Chaitanya

Reputation: 3638

Download pdf file from s3 using akka-stream-alpakka and store it as an array of bytes

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.

Upvotes: 0

Views: 412

Answers (1)

YouXiang-Wang
YouXiang-Wang

Reputation: 1127

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()

  }
}

Upvotes: 1

Related Questions