Skip to content

Commit

Permalink
Add the option to get a public URL
Browse files Browse the repository at this point in the history
We've added an option to get the public url of the object instead of the
presigned one, by sending to `get-object-url` the `:object-public-url?` key inside `opts` as true.
  • Loading branch information
MikelElizondo committed Mar 18, 2024
1 parent da8ced3 commit 9f470a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[com.amazonaws/aws-java-sdk-s3 "1.12.463"]
[javax.xml.bind/jaxb-api "2.3.1"]
[integrant "0.8.0"]
[dev.gethop/object-storage.core "0.1.4"]]
[dev.gethop/object-storage.core "0.1.5"]
[lambdaisland/uri "1.19.155"]]
:deploy-repositories [["snapshots" {:url "https://clojars.org/repo"
:username :env/CLOJARS_USERNAME
:password :env/CLOJARS_PASSWORD
Expand Down
18 changes: 16 additions & 2 deletions src/dev/gethop/object_storage/s3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[amazonica.core :refer [ex->map]]
[clojure.spec.alpha :as s]
[dev.gethop.object-storage.core :as core]
[integrant.core :as ig])
[integrant.core :as ig]
[lambdaisland.uri :refer [map->query-string query-map uri]])
(:import [com.amazonaws.services.s3.model ResponseHeaderOverrides]
[java.net URL]
[java.util Date]))
Expand Down Expand Up @@ -161,6 +162,16 @@
(.withContentType content-type)
(.withContentDisposition cd))))

(defn- presigned-url->public-url
[presigned-url]
(let [filtered-query-string (-> (query-map presigned-url {})
(select-keys [:response-content-disposition :response-content-type])
map->query-string)
public-uri (assoc (uri presigned-url)
:fragment nil
:query filtered-query-string)]
(str public-uri)))

(defn- get-object-url*
"Generates a url allowing access to the object without the need to auth oneself.
Uses the object with key `object-id` from S3 bucket referenced by
Expand All @@ -176,6 +187,7 @@
content-disposition (get opts :content-disposition :attachment)
content-type (get opts :content-type "application/octet-stream")
filename (:filename opts)
object-public-url? (:object-public-url? opts)
request {:bucket-name (:bucket-name this)
:key object-id
:expiration expiration}
Expand All @@ -193,7 +205,9 @@
request))]
;; generatePresignedUrl either succeeds or throws an exception
{:success? true
:object-url (.toString ^URL presigned-url)})
:object-url (if object-public-url?
(presigned-url->public-url (.toString ^URL presigned-url))
(.toString ^URL presigned-url))})
(catch Exception e
(ex->result e))))

Expand Down

0 comments on commit 9f470a2

Please sign in to comment.