Skip to content

Commit 52ee43b

Browse files
committed
Extract aws invoke wrapper & error handler to util
This allows us to use it other places.
1 parent daba56d commit 52ee43b

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

src/clojars/aws_util.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(ns clojars.aws-util
2+
(:require
3+
[cognitect.aws.client.api :as aws]))
4+
5+
(defn- throw-on-error
6+
[v]
7+
(if (some? (:cognitect.anomalies/category v))
8+
(throw (ex-info "AWS request failed" v))
9+
v))
10+
11+
(defn invoke
12+
[client op request]
13+
(throw-on-error (aws/invoke client {:op op :request request})))
14+
15+
(defn not-found->nil
16+
[v]
17+
(when (not= :cognitect.anomalies/not-found (:cognitect.anomalies/category v))
18+
v))

src/clojars/s3.clj

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns clojars.s3
22
(:require
3+
[clojars.aws-util :as aws-util]
34
[clojure.java.io :as io]
45
[clojure.string :as str]
56
[cognitect.aws.client.api :as aws]
@@ -20,22 +21,13 @@
2021
(-list-objects [client prefix])
2122
(-put-object [client key stream opts]))
2223

23-
(defn- throw-on-error
24-
[v]
25-
(if (some? (:cognitect.anomalies/category v))
26-
(throw (ex-info "S3 request failed" v))
27-
v))
28-
2924
(defn- list-objects-chunk
3025
[client bucket-name prefix delimeter continuation-token]
3126
(let [request (cond-> {:Bucket bucket-name}
3227
continuation-token (assoc :ContinuationToken continuation-token)
3328
delimeter (assoc :Delimiter delimeter)
3429
prefix (assoc :Prefix prefix))]
35-
(throw-on-error
36-
(aws/invoke client
37-
{:op :ListObjectsV2
38-
:request request}))))
30+
(aws-util/invoke client :ListObjectsV2 request)))
3931

4032
(defn- list-objects-seq
4133
"Generates a lazy seq of list-objects results, chunked by the API's paging."
@@ -55,37 +47,23 @@
5547
(when m
5648
(update m :ETag #(str/replace % "\"" ""))))
5749

58-
(defn- not-found->nil
59-
[v]
60-
(when (not= :cognitect.anomalies/not-found (:cognitect.anomalies/category v))
61-
v))
62-
6350
(defrecord S3Client [s3 bucket-name]
6451
S3Bucket
6552
(-delete-object [_ key]
66-
(->> {:op :DeleteObject
67-
:request {:Bucket bucket-name
68-
:Key key}}
69-
(aws/invoke s3)
70-
(throw-on-error)))
53+
(aws-util/invoke s3 :DeleteObject {:Bucket bucket-name
54+
:Key key}))
7155

7256
(-get-object-details [_ key]
73-
(->> {:op :HeadObject
74-
:request {:Bucket bucket-name
75-
:Key key}}
76-
(aws/invoke s3)
77-
(not-found->nil)
78-
(throw-on-error)
79-
(strip-etag)))
57+
(-> (aws-util/invoke s3 :HeadObject {:Bucket bucket-name
58+
:Key key})
59+
(aws-util/not-found->nil)
60+
(strip-etag)))
8061

8162
(-get-object-stream [_ key]
82-
(->> {:op :GetObject
83-
:request {:Bucket bucket-name
84-
:Key key}}
85-
(aws/invoke s3)
86-
(not-found->nil)
87-
(throw-on-error)
88-
:Body))
63+
(-> (aws-util/invoke s3 :GetObject {:Bucket bucket-name
64+
:Key key})
65+
(aws-util/not-found->nil)
66+
:Body))
8967

9068
(-list-entries [_ prefix]
9169
(sequence
@@ -101,13 +79,11 @@
10179
(list-objects-seq s3 bucket-name {:prefix prefix})))
10280

10381
(-put-object [_ key stream opts]
104-
(->> {:op :PutObject
105-
:request (merge {:Bucket bucket-name
106-
:Key key
107-
:Body stream}
108-
opts)}
109-
(aws/invoke s3)
110-
(throw-on-error))))
82+
(aws-util/invoke s3 :PutObject
83+
(merge {:Bucket bucket-name
84+
:Key key
85+
:Body stream}
86+
opts))))
11187

11288
(defn s3-client
11389
;; Credentials are derived from the instance's role when running in

0 commit comments

Comments
 (0)