Skip to content

Instantly share code, notes, and snippets.

@drankard
Created July 4, 2017 10:07
Show Gist options
  • Save drankard/2b44765ce5fb4240e547b987bf8e0913 to your computer and use it in GitHub Desktop.
Save drankard/2b44765ce5fb4240e547b987bf8e0913 to your computer and use it in GitHub Desktop.
Sign url til s3
(defn sign-url [{:keys [s3-bucket s3-key http-method-str content-type] :as all}]
(let [s3-key (if (string? s3-key)
s3-key
(str "tmp-" (java.util.UUID/randomUUID) ".json"))
http-method-str (or http-method-str "GET")
content-type (or content-type "application/octet-stream")
expires (-> 3 t/hours t/from-now to-date)
s3-client (AmazonS3Client. (DefaultAWSCredentialsProviderChain.))
s3-url (.getResourceUrl s3-client s3-bucket s3-key)
url (.generatePresignedUrl s3-client
(doto (GeneratePresignedUrlRequest. s3-bucket s3-key)
(.setExpiration expires)
(.setMethod ({"GET" HttpMethod/GET "PUT" HttpMethod/PUT} http-method-str))
(.setContentType content-type)))]
(assoc all :signed-url (.toString url)
:s3-key s3-key
:s3-url s3-url
:content-type content-type
:http-method-str http-method-str)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment