|
1 | 1 | (ns clojars.s3 |
2 | 2 | (:require |
| 3 | + [clojars.aws-util :as aws-util] |
3 | 4 | [clojure.java.io :as io] |
4 | 5 | [clojure.string :as str] |
5 | 6 | [cognitect.aws.client.api :as aws] |
|
20 | 21 | (-list-objects [client prefix]) |
21 | 22 | (-put-object [client key stream opts])) |
22 | 23 |
|
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 | | - |
29 | 24 | (defn- list-objects-chunk |
30 | 25 | [client bucket-name prefix delimeter continuation-token] |
31 | 26 | (let [request (cond-> {:Bucket bucket-name} |
32 | 27 | continuation-token (assoc :ContinuationToken continuation-token) |
33 | 28 | delimeter (assoc :Delimiter delimeter) |
34 | 29 | 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))) |
39 | 31 |
|
40 | 32 | (defn- list-objects-seq |
41 | 33 | "Generates a lazy seq of list-objects results, chunked by the API's paging." |
|
55 | 47 | (when m |
56 | 48 | (update m :ETag #(str/replace % "\"" "")))) |
57 | 49 |
|
58 | | -(defn- not-found->nil |
59 | | - [v] |
60 | | - (when (not= :cognitect.anomalies/not-found (:cognitect.anomalies/category v)) |
61 | | - v)) |
62 | | - |
63 | 50 | (defrecord S3Client [s3 bucket-name] |
64 | 51 | S3Bucket |
65 | 52 | (-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})) |
71 | 55 |
|
72 | 56 | (-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))) |
80 | 61 |
|
81 | 62 | (-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)) |
89 | 67 |
|
90 | 68 | (-list-entries [_ prefix] |
91 | 69 | (sequence |
|
101 | 79 | (list-objects-seq s3 bucket-name {:prefix prefix}))) |
102 | 80 |
|
103 | 81 | (-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)))) |
111 | 87 |
|
112 | 88 | (defn s3-client |
113 | 89 | ;; Credentials are derived from the instance's role when running in |
|
0 commit comments