-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Main.hs
60 lines (49 loc) · 1.99 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Conduit ( runConduit, (.|) )
import Data.Conduit.Binary ( sourceHandle )
import Data.Functor ( (<&>) )
import Data.Text ( pack )
import Amazonka.S3.StreamingUpload
( UploadLocation(FP), abortAllUploads, concurrentUpload, streamUpload )
import Amazonka.Auth ( discover )
import Amazonka.Env ( newEnv )
import Amazonka.S3.CreateMultipartUpload ( newCreateMultipartUpload )
import Amazonka.S3.Types ( BucketName(..), ObjectKey(..) )
import Control.Monad.IO.Class ( liftIO )
import Control.Monad.Trans.Resource ( runResourceT )
import System.Environment ( getArgs )
import System.IO ( BufferMode(BlockBuffering), hSetBuffering, stdin )
main :: IO ()
main = do
args <- getArgs
env <- newEnv discover
case args of
("upload":bucket:key:file:_) -> do
let buck = BucketName $ pack bucket
ky = ObjectKey $ pack key
hSetBuffering stdin (BlockBuffering Nothing)
res <- runResourceT $ case file of
"-" -> runConduit (sourceHandle stdin .| streamUpload env Nothing (newCreateMultipartUpload buck ky))
>>= liftIO . either print print
_ -> concurrentUpload env Nothing Nothing (FP file) (newCreateMultipartUpload buck ky)
>>= liftIO . print
print res
("abort":bucket:_) -> do
res <- runResourceT $ abortAllUploads env (BucketName $ pack bucket)
print res
_ -> usage
usage :: IO ()
usage = putStrLn . unlines $
[ "Usage:"
, ""
, " Upload file:"
, " s3upload upload <bucket> <object key> <file to upload> OR \"-\" for stdin"
, ""
, " Abort all unfinished uploads for bucket:"
, " s3upload abort <bucket>"
, ""
, "Uses `newEnv discover` to make the Amazonka environment, so it wil look at"
, "appropriate env vars, or ~/.aws/credentials, etc."
]