-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.clj
218 lines (192 loc) · 7.87 KB
/
s3.clj
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/
(ns magnet.object-storage.s3
(:require [amazonica.aws.s3 :as aws-s3]
[amazonica.core :refer [ex->map]]
[clojure.spec.alpha :as s]
[integrant.core :as ig]
[magnet.object-storage.core :as core])
(:import [com.amazonaws.services.s3.model ResponseHeaderOverrides]
[java.util Date]))
(def ^:const default-presigned-url-lifespan
"Default presigned urls lifespan, expressed in minutes"
60)
(defn- ex->result
"Create a result map from `e` exception details"
[e]
(cond
(instance? com.amazonaws.AmazonServiceException e)
{:success? false
:error-details (dissoc (ex->map e) :stack-trace)}
true
(let [error-details {:message (.getMessage e)}]
{:success? false
:error-details (cond-> error-details
(instance? com.amazonaws.AmazonClientException e)
(assoc :error-type "Client"))})))
(s/def ::AWSS3Bucket (s/keys :req-un [::bucket-name ::presigned-url-lifespan]))
(defn- put-object*
"Put `object` in the S3 bucket referenced by `this`, using `object-id` as the key.
Use `opts` to specify additional put options.
`object` can be either a File object or an InputStream. In the
latter case, if you know the size of the content in the InputStream,
add the `:metadata` key to the `opts` map. Its value should be a map
with a key called `:object-size`, with the size as its value."
[this object-id object opts]
{:pre [(and (s/valid? ::AWSS3Bucket this)
(s/valid? ::core/object-id object-id)
(s/valid? ::core/object object)
(s/valid? ::core/put-object-opts opts))]}
(try
(let [metadata (:metadata opts)
encryption (:encryption opts)
request {:bucket-name (:bucket-name this)
:key object-id
:file object}
request (cond-> request
(instance? java.io.InputStream object)
(->
(dissoc :file)
(assoc :input-stream object)
(assoc-in [:metadata :content-length] (:object-size metadata)))
encryption
(assoc :encryption encryption))]
;; putObject either succeeds or throws an exception
(aws-s3/put-object request)
{:success? true})
(catch Exception e
(ex->result e))))
(s/fdef put-object*
:args ::core/put-object-args
:ret ::core/put-object-ret)
(defn- get-object*
"Get the object with key `object-id` from S3 bucket referenced by `this`, using `opts` options"
[this object-id opts]
{:pre [(and (s/valid? ::AWSS3Bucket this)
(s/valid? ::core/object-id object-id)
(s/valid? ::core/get-object-opts opts))]}
(try
(let [encryption (:encryption opts)
request {:bucket-name (:bucket-name this)
:key object-id}
request (cond-> request
encryption
(assoc :encryption encryption))
result (aws-s3/get-object request)]
;; getObject can return null in some cases. Quoting
;; documentation "When specifying constraints in the request
;; object, the client needs to be prepared to handle this method
;; returning null if the provided constraints aren't met when
;; Amazon S3 receives the request."
(if result
{:success? true
:object (:input-stream result)}
{:success? false
:error-details {:error-code "RequestConstraintsNotMet"}}))
(catch Exception e
(ex->result e))))
(s/fdef get-object*
:args ::core/get-object-args
:ret ::core/get-object-ret)
(defn- kw->http-method
[k]
{:pre [(s/valid? ::core/method k)]}
(k {:create "PUT" :read "GET", :update "PUT", :delete "DELETE"}))
(defn- attachment-header
[filename]
(->
(ResponseHeaderOverrides.)
(.withContentDisposition (str "attachment; filename=" filename))))
(defn- get-object-url*
"Generates a url allowing access to the object without the need to auth oneself.
Get the object with key `object-id` from S3 bucket referenced by
`this`, using `opts` options"
[this object-id opts]
{:pre [(and (s/valid? ::AWSS3Bucket this)
(s/valid? ::core/object-id object-id)
(s/valid? ::core/get-object-url-opts opts))]}
(try
(let [expiration (Date. (+ (System/currentTimeMillis)
(int (* (:presigned-url-lifespan this) 60 1000))))
method (:method opts)
filename (:filename opts)
request {:bucket-name (:bucket-name this)
:key object-id
:expiration expiration}
request (cond-> request
method
(assoc :method (kw->http-method method))
filename
(assoc :response-headers (attachment-header filename)))
presigned-url (->
request
(aws-s3/generate-presigned-url)
.toString)]
;; generatePresignedUrl either succeeds or throws an exception
{:success? true
:object-url presigned-url})
(catch Exception e
(ex->result e))))
(s/fdef get-object-url*
:args ::core/get-object-url-args
:ret ::core/get-object-url-ret)
(defn- delete-object*
"Delete the object `object-id` from S3 bucket referenced by `this`
Use `opts` to specify additional delete options."
[this object-id opts]
{:pre [(and (s/valid? ::AWSS3Bucket this)
(s/valid? ::core/object-id object-id)
(s/valid? ::core/delete-object-opts opts))]}
(try
;; deleteObject either succeeds or throws an exception
(aws-s3/delete-object {:bucket-name (:bucket-name this), :key object-id})
{:success? true}
(catch Exception e
(ex->result e))))
(s/fdef delete-object*
:args ::core/delete-object-args
:ret ::core/delete-object-ret)
(defn- list-objects*
[this parent-object-id]
{:pre [(and (s/valid? ::AWSS3Bucket this)
(s/valid? ::core/object-id parent-object-id))]}
(try
(let [result (aws-s3/list-objects-v2 {:bucket-name (:bucket-name this)
:prefix (str parent-object-id)})]
{:success? true
:objects (->> result
:object-summaries
(map (fn [{:keys [key last-modified size]}]
{:object-id key
:last-modified last-modified
:size size})))})
(catch Exception e
(ex->result e))))
(s/fdef list-objects*
:args ::core/list-objects-args
:ret ::core/list-objects-ret)
(defrecord AWSS3Bucket [bucket-name presigned-url-lifespan]
core/ObjectStorage
(put-object [this object-id object]
(put-object* this object-id object {}))
(put-object [this object-id object opts]
(put-object* this object-id object opts))
(get-object [this object-id]
(get-object* this object-id {}))
(get-object [this object-id opts]
(get-object* this object-id opts))
(get-object-url [this object-id]
(get-object-url* this object-id {}))
(get-object-url [this object-id opts]
(get-object-url* this object-id opts))
(delete-object [this object-id]
(delete-object* this object-id {}))
(delete-object [this object-id opts]
(delete-object* this object-id opts))
(list-objects [this parent-object-id]
(list-objects* this parent-object-id)))
(defmethod ig/init-key :magnet.object-storage/s3 [_ {:keys [bucket-name presigned-url-lifespan]
:or {presigned-url-lifespan default-presigned-url-lifespan}}]
(map->AWSS3Bucket {:bucket-name bucket-name
:presigned-url-lifespan presigned-url-lifespan}))