Play 2.x : Reactive file upload with Iteratees

后端 未结 4 893
北恋
北恋 2020-12-04 10:18

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 thi

4条回答
  •  Happy的楠姐
    2020-12-04 10:59

    For those who are also trying to figure out a solution of this streaming problem, instead of writing a whole new BodyParser, you can also use what has already been implemented in parse.multipartFormData. You can implement something like below to overwrite the default handler handleFilePartAsTemporaryFile.

    def handleFilePartAsS3FileUpload: PartHandler[FilePart[String]] = {
      handleFilePart {
        case FileInfo(partName, filename, contentType) =>
    
          (rechunkAdapter &>> writeToS3).map {
            _ =>
              val compRequest = new CompleteMultipartUploadRequest(...)
              amazonS3Client.completeMultipartUpload(compRequest)
              ...
          }
      }
    }
    
    def multipartFormDataS3: BodyParser[MultipartFormData[String]] = multipartFormData(handleFilePartAsS3FileUpload)
    

    I am able to make this work but I am still not sure whether the whole upload process is streamed. I tried some large files, it seems the S3 upload only starts when the whole file has been sent from the client side.

    I looked at the above parser implementation and I think everything is connected using Iteratee so the file should be streamed. If someone has some insight on this, that will be very helpful.

提交回复
热议问题