Skip to content
Permalink
Browse files

Add WithManager variants

  • Loading branch information...
bgamari committed Jun 6, 2016
1 parent e0eb0a2 commit 598cb0ea1c43b8a11f423e849af047756296c723
Showing with 57 additions and 10 deletions.
  1. +57 −10 src/Pipes/Aws/S3.hs
@@ -8,12 +8,16 @@ module Pipes.Aws.S3
-- * Downloading
, fromS3
, fromS3'
, fromS3WithManager
-- ** Convenient re-exports
, responseBody
-- * Uploading
-- | These internally use the S3 multi-part upload interface to achieve
-- streaming upload behavior.
, ChunkSize
, toS3
, toS3'
, toS3WithManager
) where

import Control.Monad (unless)
@@ -55,6 +59,9 @@ newtype Object = Object T.Text
-- 'fromS3' bucket object responseBody
-- @
--
-- Note that this makes no attempt at reusing a 'Manager' and therefore may not
-- be very efficient for many small requests. See 'fromS3WithManager' for more
-- control over the 'Manager' used.
fromS3 :: MonadSafe m
=> Bucket -> Object
-> (Response (Producer BS.ByteString m ()) -> Producer BS.ByteString m a)
@@ -65,13 +72,37 @@ fromS3 bucket object handler = do

-- | Download an object from S3 explicitly specifying an @aws@ 'Aws.Configuration',
-- which provides credentials and logging configuration.
--
-- Note that this makes no attempt at reusing a 'Manager' and therefore may not
-- be very efficient for many small requests. See 'fromS3WithManager' for more
-- control over the 'Manager' used.
fromS3' :: MonadSafe m
=> Aws.Configuration -> Bucket -> Object
-> (Response (Producer BS.ByteString m ()) -> Producer BS.ByteString m a)
-> Producer BS.ByteString m a
fromS3' cfg (Bucket bucket) (Object object) handler = do
let s3cfg = Aws.defServiceConfig :: S3.S3Configuration Aws.NormalQuery
fromS3' cfg bucket object handler = do
mgr <- liftIO $ newManager tlsManagerSettings
fromS3WithManager mgr cfg bucket object handler

-- | Download an object from S3 explicitly specifying an @http-client@ 'Manager'
-- and @aws@ 'Aws.Configuration' (which provides credentials and logging
-- configuration).
--
-- This can be more efficient when submitting many small requests as it allows
-- re-use of the 'Manager' across requests. Note that the 'Manager' provided
-- must support TLS; such a manager can be created with
--
-- @
-- 'newManager' 'HTTP.Client.TLS.tlsManagerSettings'
-- @
fromS3WithManager
:: MonadSafe m
=> Manager
-> Aws.Configuration -> Bucket -> Object
-> (Response (Producer BS.ByteString m ()) -> Producer BS.ByteString m a)
-> Producer BS.ByteString m a
fromS3WithManager mgr cfg (Bucket bucket) (Object object) handler = do
let s3cfg = Aws.defServiceConfig :: S3.S3Configuration Aws.NormalQuery
req <- liftIO $ buildRequest cfg s3cfg $ S3.getObject bucket object
Pipes.Safe.bracket (liftIO $ responseOpen req mgr) (liftIO . responseClose) $ \resp ->
handler $ resp { responseBody = from $ brRead $ responseBody resp }
@@ -118,8 +149,7 @@ type ChunkSize = Int
type ETag = T.Text
type PartN = Integer

-- | Upload content to an S3 object explicitly specifying an @aws@
-- 'Aws.Configuration', which provides credentials and logging configuration.
-- | Upload content to an S3 object.
toS3 :: forall m a. MonadIO m
=> ChunkSize -> Bucket -> Object
-> Producer BS.ByteString m a
@@ -128,17 +158,33 @@ toS3 chunkSize bucket object consumer = do
cfg <- Aws.baseConfiguration
toS3' cfg chunkSize bucket object consumer

-- | Upload content to an S3 object.
--
-- This internally uses the S3 multi-part upload interface to achieve streaming
-- upload behavior.
-- | Upload content to an S3 object, explicitly specifying an
-- 'Aws.Configuration', which provides credentials and logging configuration.
toS3' :: forall m a. MonadIO m
=> Aws.Configuration -> ChunkSize -> Bucket -> Object
-> Producer BS.ByteString m a
-> m a
toS3' cfg chunkSize (Bucket bucket) (Object object) consumer = do
let s3cfg = Aws.defServiceConfig :: S3.S3Configuration Aws.NormalQuery
toS3' cfg chunkSize bucket object consumer = do
mgr <- liftIO $ newManager tlsManagerSettings
toS3WithManager mgr cfg chunkSize bucket object consumer

-- | Download an object from S3 explicitly specifying an @http-client@ 'Manager'
-- and @aws@ 'Aws.Configuration' (which provides credentials and logging
-- configuration).
--
-- This can be more efficient when submitting many small requests as it allows
-- re-use of the 'Manager' across requests. Note that the 'Manager' provided
-- must support TLS; such a manager can be created with
--
-- @
-- 'newManager' 'HTTP.Client.TLS.tlsManagerSettings'
-- @
toS3WithManager :: forall m a. MonadIO m
=> Manager -> Aws.Configuration -> ChunkSize -> Bucket -> Object
-> Producer BS.ByteString m a
-> m a
toS3WithManager mgr cfg chunkSize (Bucket bucket) (Object object) consumer = do
let s3cfg = Aws.defServiceConfig :: S3.S3Configuration Aws.NormalQuery

resp1 <- liftIO $ runResourceT
$ Aws.pureAws cfg s3cfg mgr
@@ -161,6 +207,7 @@ toS3' cfg chunkSize (Bucket bucket) (Object object) consumer = do
$ S3.postCompleteMultipartUpload bucket object uploadId parts
return res


enumFromP :: (Monad m, Enum i) => i -> Pipe a (i, a) m r
enumFromP = go
where

0 comments on commit 598cb0e

Please sign in to comment.
You can’t perform that action at this time.