Stream a file to AWS S3 using Akka Streams

In this blog, I’ll show you how to stream a file to AWS S3 using the library –

To use the above, add to your dependencies

"com.gilt" %% "gfc-aws-s3" % "0.1.0"

The library contains akka-stream Sources and Sinks to Stream data from and to S3

From my blog on “Upload file in Akka Http“, you can see how to obtain the stream of a file, So once you get that source its pretty simple to stream it to AWS S3.

Let’s first create our AWS S3 client, here we are using Amazon SDK

So we pass this s3Client, in our S3ClientService as shown below, with the name of the bucket that we want the service to work with.

In this service, we have a write method, just supply the path where you want to store the file and source to file to this, it, in turn, is calling getSink method that takes path as parameter and it will upload your file as a stream to AWS S3, so here the getSink method creates a Sink using S3MultipartUploaderSink of gfc-aws-s3 library, and as that Sink accepts Byte we modify our stream of ByteString to Byte using mapConcat with identity.

Now, Let’s see how we will use this service to stream a file from the browser to Amazon S3 using Akka-Http route.

In this example, we are uploading a picture to S3 using the service we created above.
The library also has S3MultipartDownload and S3ChunkedDownload to download files from S3, will go through them in our next blog.
Hopefully, this was helpful and you guys can use it if stuck in a similar use case.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!