diff options
author | Alex <alex@adnab.me> | 2024-01-11 10:52:12 +0000 |
---|---|---|
committer | Alex <alex@adnab.me> | 2024-01-11 10:52:12 +0000 |
commit | 723e56b37f13f078a15e067343191fb1bf96e8b2 (patch) | |
tree | 8188e7e8bc41867ce2de61cd7df6554e05678d11 /script/jepsen.garage/src/jepsen/garage/s3api.clj | |
parent | a8b0e01f88b947bc34c05d818d51860b4d171967 (diff) | |
parent | fa9247f11b89c960dffe82d6bf990ed4335788e3 (diff) | |
download | garage-723e56b37f13f078a15e067343191fb1bf96e8b2.tar.gz garage-723e56b37f13f078a15e067343191fb1bf96e8b2.zip |
Merge pull request 'Jepsen testing (NLnet task 3 subtask 1)' (#544) from jepsen into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/544
Diffstat (limited to 'script/jepsen.garage/src/jepsen/garage/s3api.clj')
-rw-r--r-- | script/jepsen.garage/src/jepsen/garage/s3api.clj | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/script/jepsen.garage/src/jepsen/garage/s3api.clj b/script/jepsen.garage/src/jepsen/garage/s3api.clj new file mode 100644 index 00000000..531e0157 --- /dev/null +++ b/script/jepsen.garage/src/jepsen/garage/s3api.clj @@ -0,0 +1,48 @@ +(ns jepsen.garage.s3api + (:require [clojure.tools.logging :refer :all] + [jepsen [control :as c]] + [amazonica.aws.s3 :as s3] + [slingshot.slingshot :refer [try+]])) + +; GARAGE S3 HELPER FUNCTIONS + +(defn get + "Helper for GetObject" + [creds k] + (try+ + (-> (s3/get-object creds (:bucket creds) k) + :input-stream + slurp) + (catch (re-find #"Key not found" (.getMessage %)) ex + nil))) + +(defn put + "Helper for PutObject or DeleteObject (is a delete if value is nil)" + [creds k v] + (if (= v nil) + (s3/delete-object creds + :bucket-name (:bucket creds) + :key k) + (let [some-bytes (.getBytes v "UTF-8") + bytes-stream (java.io.ByteArrayInputStream. some-bytes)] + (s3/put-object creds + :bucket-name (:bucket creds) + :key k + :input-stream bytes-stream + :metadata {:content-length (count some-bytes)})))) + +(defn list-inner [creds prefix ct accum] + (let [list-result (s3/list-objects-v2 creds + {:bucket-name (:bucket creds) + :prefix prefix + :continuation-token ct}) + new-object-summaries (:object-summaries list-result) + new-objects (map (fn [d] (:key d)) new-object-summaries) + objects (concat new-objects accum)] + (if (:truncated? list-result) + (list-inner creds prefix (:next-continuation-token list-result) objects) + objects))) +(defn list + "Helper for ListObjects -- just lists everything in the bucket" + [creds prefix] + (list-inner creds prefix nil [])) |