Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming from S3 bucket using Amazonka with Servant

I'd like to be able to stream S3 bucket object contents via Servant as the response body.

I'm having issues getting MonadResource instance missing for Handler:

src/Servant/Streaming/Example.hs:29:3: error:
    * No instance for (MonadResource Handler)
        arising from a use of `runAWS'
    * In a stmt of a 'do' block: runAWS env conduits
      In the expression:
        do env <- newEnv Discover
           runAWS env conduits
      In an equation for `server':
          server
            = do env <- newEnv Discover
                 runAWS env conduits
   |
29 |   runAWS env conduits
   |   ^^^^^^^^^^^^^^^^^^^

I have made a repository to reproduce: https://github.com/domenkozar/servant-streaming-amazonka

servant-streaming-server handles ResourceT for Stream (Of BS.ByteString) (ResourceT IO) () https://github.com/plow-technologies/servant-streaming/blob/master/servant-streaming-server/src/Servant/Streaming/Server/Internal.hs#L77-L79

but since I'm using Amazonka I also need to make sure MonadResource for Handler is taken take in that bracket. It's not clear to me how to do that.

My understanding is that using enter/hoistServer this won't work since resources would be cleaned up too soon (before streaming).

Notes:

  • This builds upon question asked in 2016: Haskell Servant and streaming
  • Servant part is implemented via https://github.com/plow-technologies/servant-streaming/pull/2/files

Edits

  1. EDIT: I've since replaced $$ with $$+-
  2. EDIT2: I've resolved Conduit specific errors, now fighting with MonadResource
like image 585
iElectric Avatar asked Nov 07 '22 09:11

iElectric


1 Answers

Solved with

server :: Server API
server =  do
  env <- newEnv Discover
  res <- runInternalState (runAWS env conduits) st
  return (res >> liftIO (closeInternalState st))

https://github.com/domenkozar/servant-streaming-amazonka/commit/c5fad78dd7bf733cecb8790035105c819d5f5ae9

like image 162
iElectric Avatar answered Dec 09 '22 10:12

iElectric