Having touched Haskell since 2013, I am writing a small Web.Scotty service to manage S3 bucket (with Amazonka-2.0).
The Web.Scotty part and Amazonka was pretty clear, but I am not sure how to make it work together:
main :: IO ()
main = do
env <- Amazonka.newEnv Amazonka.discover
scotty 3000 (app env)
app :: Amazonka.Env -> ScottyM ()
app env = do
get "/stream-file" $ do
runResourceT $ do
resp <- runResourceT $ Amazonka.send env (newGetObject "bucket" "file")
(resp ^. getObjectResponse_body) `sinkBody` (CC.map fromByteString .| CC.mapM_ (liftIO . print))
lift $ stream $ \send flush -> do
(resp ^. getObjectResponse_body) `sinkBody` (CC.map fromByteString .| CC.mapM_ (liftIO . send) >> liftIO flush)
I tried removing runResourceT in here, without any change:
resp <- Amazonka.send env (newGetObject "bucket" "file")
This works and prints to console successfully:
(resp ^. getObjectResponse_body) `sinkBody` (CC.map fromByteString .| CC.mapM_ (liftIO . print))
This doesn't work (if print section is commented out) with an error:
lift $ stream $ \send flush -> do
(resp ^. getObjectResponse_body) `sinkBody` (CC.map fromByteString .| CC.mapM_ (liftIO . send) >> liftIO flush)
Error:
HttpExceptionRequest Request {
host = "bucket.s3.us-east-1.amazonaws.com"
port = 443
secure = True
requestHeaders = [("X-Amz-Content-SHA256",""),("X-Amz-Date",""),("Host","bucket.s3.us-east-1.amazonaws.com"),("Authorization","<REDACTED>")]
path = "/file"
queryString = ""
method = "GET"
proxy = Nothing
rawBody = False
redirectCount = 0
responseTimeout = ResponseTimeoutMicro 70000000
requestVersion = HTTP/1.1
proxySecureMode = ProxySecureWithConnect
}
ConnectionClosed
What am I missing?
If you try:
you'll see:
which shows that
streamindeed only "sets up" the pipeline, but it's actually executed after the handler completes i.e. after your resources are deallocated (in your case the connection to AWS).