/
S3.purs
199 lines (159 loc) · 5.22 KB
/
S3.purs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
module AWS.S3
( S3
, BucketKey(..)
, BucketName(..)
, BucketPolicyParams
, BucketPolicy(..)
, createBucket
, CreateBucketResponse
, getObject
, GetObjectParams
, GetObjectResponse
, makeClient
, putBucketPolicy
, GetSignedUrlParams
, Operation(..)
, SignedUrl(..)
, getSignedUrl
) where
import AWS.Core.Client (makeClientHelper)
import AWS.Core.Types (DefaultClientProps, Region(..), Tags)
import AWS.Core.Util (raiseEither)
import Control.Bind ((>>=))
import Control.Promise (Promise, toAffE)
import Data.Argonaut (Json, JsonDecodeError, decodeJson)
import Data.Bifunctor (lmap)
import Data.Either (Either)
import Data.Function.Uncurried (Fn2, runFn2, Fn3, runFn3)
import Data.Int (round)
import Data.Newtype (class Newtype)
import Data.Show (show)
import Data.Time.Duration (Seconds(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Justifill (justifillVia)
import Justifill.Fillable (class Fillable)
import Justifill.Justifiable (class Justifiable)
import Node.Buffer (Buffer)
import Prelude (class Show, Unit, (#), ($), (<#>), (>>>))
import Type.Proxy (Proxy(..))
foreign import data S3 :: Type
foreign import newS3 :: Json -> Effect S3
makeClient ::
forall r via.
Justifiable { | r } { | via } =>
Fillable { | via } DefaultClientProps =>
{ | r } ->
Effect S3
makeClient r = makeClientHelper newS3 props
where
viaProxy :: Proxy { | via }
viaProxy = Proxy
props :: DefaultClientProps
props = justifillVia viaProxy r
newtype BucketName
= BucketName String
derive instance ntBucketName :: Newtype BucketName _
derive newtype instance showBucketName :: Show BucketName
type InternalGetObjectParams
= { "Bucket" :: String
, "Key" :: String
}
type InternalGetObjectResponse
= { "Body" :: Buffer
, "ContentLength" :: Int
, "ContentEncoding" :: String
, "ContentType" :: String
, "Metadata" :: Json
}
foreign import getObjectImpl :: Fn2 S3 InternalGetObjectParams (Effect (Promise InternalGetObjectResponse))
newtype BucketKey
= BucketKey String
derive instance ntBucketKey :: Newtype BucketKey _
type GetObjectParams
= { bucket :: BucketName
, key :: BucketKey
}
type GetObjectResponse
= { body :: Buffer
, contentLength :: Int
, contentEncoding :: String
, contentType :: String
, tags :: Tags
}
getObject :: S3 -> GetObjectParams -> Aff GetObjectResponse
getObject client { bucket: BucketName name, key: BucketKey key } =
runFn2 getObjectImpl client params
# toAffE
>>= (convert >>> lmap show >>> raiseEither)
where
params =
{ "Bucket": name
, "Key": key
}
convert :: InternalGetObjectResponse -> Either JsonDecodeError GetObjectResponse
convert internalResponse = parse internalResponse."Metadata" <#> addTags
where
addTags tags =
{ body: internalResponse."Body"
, contentLength: internalResponse."ContentLength"
, contentEncoding: internalResponse."ContentEncoding"
, contentType: internalResponse."ContentType"
, tags: tags
}
parse :: Json -> Either JsonDecodeError Tags
parse = decodeJson
type InternalGetSignedUrlParams
= { "Bucket" :: String
, "Key" :: String
, "Expires" :: Int
}
type GetSignedUrlParams
= { bucket :: BucketName
, key :: BucketKey
, expires :: Seconds
}
data Operation
= GetObject
| PutObject
newtype SignedUrl
= SignedUrl String
derive instance ntSignedUrl :: Newtype SignedUrl _
derive newtype instance showSignedUrl :: Show SignedUrl
foreign import getSignedUrlImpl :: Fn3 S3 String InternalGetSignedUrlParams (Effect (Promise String))
getSignedUrl :: S3 -> Operation -> GetSignedUrlParams -> Aff SignedUrl
getSignedUrl s3 operation { bucket: BucketName bucketName, key: BucketKey bucketKey, expires: Seconds seconds } = runFn3 getSignedUrlImpl s3 (toString operation) params # toAffE <#> SignedUrl
where
toString GetObject = "getObject"
toString PutObject = "putObject"
params = { "Bucket": bucketName, "Key": bucketKey, "Expires": round seconds }
foreign import createBucketImpl :: Fn3 S3 String InternalBucketConfiguration (Effect (Promise InternalCreateBucketResponse))
type InternalBucketConfiguration
= { "LocationConstraint" :: String }
type InternalCreateBucketResponse
= { "Location" :: String }
type CreateBucketResponse
= { location :: Region }
createBucket :: S3 -> BucketName -> Region -> Aff CreateBucketResponse
createBucket s3 (BucketName name) (Region region) =
runFn3 createBucketImpl s3 name bucketConfig
# toAffE
<#> convert
where
bucketConfig :: InternalBucketConfiguration
bucketConfig = { "LocationConstraint": region }
convert :: InternalCreateBucketResponse -> CreateBucketResponse
convert internalResponse = { location: Region internalResponse."Location" }
foreign import putBucketPolicyImpl :: Fn3 S3 String String (Effect (Promise Unit))
type BucketPolicyParams
= { name :: BucketName
, policy :: BucketPolicy
}
newtype BucketPolicy
= BucketPolicy String
derive instance ntBucketPolicy :: Newtype BucketPolicy _
derive newtype instance showBucketPolicy :: Show BucketPolicy
putBucketPolicy :: S3 -> BucketName -> BucketPolicy -> Aff Unit
putBucketPolicy s3 (BucketName name) (BucketPolicy policy) =
toAffE
$ runFn3 putBucketPolicyImpl s3 name policy