module Network.AWS.S3Object (
sendObject, copyObject, getObject, getObjectInfo, deleteObject,
publicUriForSeconds, publicUriUntilTime,
S3Object(..)
) where
import Network.AWS.Authentication as Auth
import Network.AWS.AWSResult
import Network.AWS.AWSConnection
import Network.HTTP
import Network.URI
import System.Time
import qualified Data.ByteString.Lazy.Char8 as L
data S3Object =
S3Object {
obj_bucket :: String,
obj_name :: String,
content_type :: String,
obj_headers :: [(String, String)],
obj_data :: L.ByteString
} deriving (Read, Show)
sendObject :: AWSConnection
-> S3Object
-> IO (AWSResult ())
sendObject aws obj =
do res <- Auth.runAction (S3Action aws (urlEncode (obj_bucket obj))
(urlEncode (obj_name obj))
""
(("Content-Type", (content_type obj)) :
obj_headers obj)
(obj_data obj) PUT)
return (either Left (\_ -> Right ()) res)
publicUriUntilTime :: AWSConnection
-> S3Object
-> Integer
-> URI
publicUriUntilTime c obj time =
let act = S3Action c (urlEncode (obj_bucket obj)) (urlEncode (obj_name obj)) "" [] L.empty GET
in preSignedURI act time
publicUriForSeconds :: AWSConnection
-> S3Object
-> Integer
-> IO URI
publicUriForSeconds c obj time =
do TOD ctS _ <- getClockTime
return (publicUriUntilTime c obj (ctS + time))
getObject :: AWSConnection
-> S3Object
-> IO (AWSResult S3Object)
getObject = getObjectWithMethod GET
getObjectInfo :: AWSConnection
-> S3Object
-> IO (AWSResult S3Object)
getObjectInfo = getObjectWithMethod HEAD
getObjectWithMethod :: RequestMethod
-> AWSConnection
-> S3Object
-> IO (AWSResult S3Object)
getObjectWithMethod m aws obj =
do res <- Auth.runAction (S3Action aws (urlEncode (obj_bucket obj))
(urlEncode (obj_name obj))
""
(obj_headers obj)
L.empty m)
return (either Left (\x -> Right (populate_obj_from x)) res)
where
populate_obj_from x =
obj { obj_data = (rspBody x),
obj_headers = (headersFromResponse x) }
headersFromResponse :: HTTPResponse L.ByteString -> [(String,String)]
headersFromResponse r =
let respheaders = rspHeaders r
in map (\x -> case x of
Header (HdrCustom name) val -> (name, (mimeDecode val))
) (filter isAmzHeader respheaders)
deleteObject :: AWSConnection
-> S3Object
-> IO (AWSResult ())
deleteObject aws obj = do res <- Auth.runAction (S3Action aws (urlEncode (obj_bucket obj))
(urlEncode (obj_name obj))
""
(obj_headers obj)
L.empty DELETE)
return (either Left (\_ -> Right ()) res)
copyObject :: AWSConnection
-> S3Object
-> S3Object
-> IO (AWSResult S3Object)
copyObject aws srcobj destobj =
do res <- Auth.runAction (S3Action aws (urlEncode (obj_bucket destobj))
(urlEncode (obj_name destobj))
""
(copy_headers)
L.empty PUT)
return (either Left (\x -> Right (populate_obj_from x)) res)
where
populate_obj_from x =
destobj { obj_data = (rspBody x),
obj_headers = (headersFromResponse x) }
copy_headers = [("x-amz-copy-source",
("/"++ (urlEncode (obj_bucket srcobj))
++ "/" ++ (urlEncode (obj_name srcobj))))]