forked from axman6/amazonka-s3-streaming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
74 lines (66 loc) · 2.84 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Conduit (($$))
import Data.Conduit.Binary (sourceHandle)
import Data.Text (pack)
import Network.AWS
import Network.AWS.Data.Text (fromText)
import Network.AWS.S3.CreateMultipartUpload
import Network.AWS.S3.StreamingUpload
import System.Environment
import System.IO (BufferMode(BlockBuffering),
hSetBuffering, stdin)
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative (pure, (<$>), (<*>))
#endif
main :: IO ()
main = do
args <- getArgs
case args of
(region:profile:credfile:bucket:key:file:_) ->
case (,,,) <$> (FromFile <$> fromText (pack profile) <*> pure credfile)
<*> (fromText (pack region) :: Either String Region)
<*> fromText (pack bucket)
<*> fromText (pack key)
of
Right (creds,_reg,buck,ky) -> do
#if !MIN_VERSION_amazonka(1,4,4)
env <- newEnv _reg creds
#else
env <- newEnv creds
#endif
hSetBuffering stdin (BlockBuffering Nothing)
res <- runResourceT . runAWS env $ case file of
-- Stream data from stdin
"-" -> sourceHandle stdin $$ streamUpload Nothing (createMultipartUpload buck ky)
-- Read from a file
_ -> concurrentUpload Nothing Nothing (FP file) $ createMultipartUpload buck ky
print res
Left err -> print err >> usage
("abort":region:profile:credfile:bucket:_) ->
case (,,) <$> (FromFile <$> fromText (pack profile) <*> pure credfile)
<*> (fromText (pack region) :: Either String Region)
<*> fromText (pack bucket)
of
Right (creds,_reg,buck) -> do
#if !MIN_VERSION_amazonka(1,4,4)
env <- newEnv _reg creds
#else
env <- newEnv creds
#endif
res <- runResourceT . runAWS env . abortAllUploads $ buck
print res
Left err -> print err >> usage
_ -> usage
usage :: IO ()
usage = putStrLn . unlines $
[ "Usage: \n"
, " Upload file:"
, " s3upload <region:ap-southeast-2> <profile> <credentials file:$HOME/.aws/credentials> <bucket> <object key> <file to upload>"
, " Abort all unfinished uploads for bucket:"
, " s3upload abort <region:ap-southeast-2> <profile> <credentials file:$HOME/.aws/credentials> <bucket>\n"
, "all arguments must be supplied - the region will be obtained from the AWS_REGION env var"
, "if compiled with amazonka > 1.4.4, but must still be supplied (making an option parsing library"
, "a dependency of this package seemed overkill)"
]