-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.hs
61 lines (51 loc) · 1.75 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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Yesod
import System.Environment
import System.IO
import Control.Lens
import Control.Monad
import Data.Text
import Data.String
import Network.AWS hiding (getEnv)
import Network.AWS.S3
import Data.Conduit
import Data.Conduit.Binary
data S3App = S3App
{ s3BucketName :: BucketName
, s3AccessKeyId :: AccessKey
, s3SecretAccessKey :: SecretKey
}
data Upload = Upload
{ uFileInfo :: FileInfo
}
mkYesod "S3App" [parseRoutes|
/ HomeR POST
|]
instance Yesod S3App
instance RenderMessage S3App FormMessage where
renderMessage _ _ = defaultFormMessage
postHomeR :: Handler Html
postHomeR = do
upload <- runInputPost $ Upload
<$> ireq fileField "file"
uploadBody upload >>= putToAws (uploadObjectName upload)
defaultLayout [whamlet|S3 upload POC!|]
uploadObjectName = fromString . unpack . fileName . uFileInfo
uploadBody upload = do
let source = fileSource $ uFileInfo upload
source $$ sinkLbs >>= return . toBody
putToAws name body = do
S3App {..} <- getYesod
s3Env <- newEnv Frankfurt $ FromKeys s3AccessKeyId s3SecretAccessKey
s3Logger <- newLogger Debug stdout
runResourceT . runAWS (s3Env & envLogger .~ s3Logger) $
void . send $ putObject s3BucketName name body
main :: IO ()
main = do
bucket <- fmap fromString $ getEnv "AWS_BUCKET_NAME"
accessKeyId <- fmap fromString $ getEnv "AWS_ACCESS_KEY_ID"
secretAccessKey <- fmap fromString $ getEnv "AWS_SECRET_ACCESS_KEY"
warp 3000 $ S3App bucket accessKeyId secretAccessKey