I will start with the question: How to use Scala API’s Iteratee to upload a file to the cloud storage (Azure Blob Storage in my case, but I don’t think it’s most important now)
Background:
I need to chunk the input into blocks of about 1 MB for storing large media files (300 MB+) as an Azure’s BlockBlobs. Unfortunately, my Scala knowledge is still poor (my project is Java based and the only use for Scala in it will be an Upload controller).
I tried with this code: Why makes calling error or done in a BodyParser's Iteratee the request hang in Play Framework 2.0? (as a Input Iteratee) – it works quite well but eachElement that I could use has size of 8192 bytes, so it’s too small for sending some hundred megabyte files to the cloud.
I must say that’s quite a new approach to me, and most probably I misunderstood something (don’t want to tell that I misunderstood everything ;> )
I will appreciate any hint or link, which will help me with that topic. If is there any sample of similar usage it would be the best option for me to get the idea.
Basically what you need first is rechunk input as bigger chunks, 1024 * 1024 bytes.
First let’s have an
Iterateethat will consume up to 1m of bytes (ok to have the last chunk smaller)Using that, we can construct an
Enumeratee(adapter) that will regroup chunks, using an API called grouped:Here grouped uses an
Iterateeto determine how much to put in each chunk. It uses the our consumeAMB for that. Which means the result is anEnumerateethat rechunks input intoArray[Byte]of 1MB.Now we need to write the
BodyParser, which will use theIteratee.foldMmethod to send each chunk of bytes:foldM passes a state along and uses it in its passed function
(S,Input[Array[Byte]]) => Future[S]to return a new Future of state. foldM will not call the function again until theFutureis completed and there is an available chunk of input.And the body parser will be rechunking input and pushing it into the store:
Returning a Right indicates that you are returning a body by the end of the body parsing (which happens to be the handler here).