From 9f470a236840eec13d272435b72ee020d1714cd7 Mon Sep 17 00:00:00 2001 From: Mikel Elizondo Date: Mon, 18 Mar 2024 16:56:32 +0100 Subject: [PATCH] Add the option to get a public URL 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. --- project.clj | 3 ++- src/dev/gethop/object_storage/s3.clj | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/project.clj b/project.clj index 35feee4..1bd0639 100644 --- a/project.clj +++ b/project.clj @@ -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 diff --git a/src/dev/gethop/object_storage/s3.clj b/src/dev/gethop/object_storage/s3.clj index 0712010..8664ef4 100644 --- a/src/dev/gethop/object_storage/s3.clj +++ b/src/dev/gethop/object_storage/s3.clj @@ -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])) @@ -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 @@ -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} @@ -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))))