@@ -5,20 +5,42 @@ defmodule Explorer.FSS do
55 # Public APIs accept URIs like "s3://bucket/key", and this module
66 # parses them into triplets {:backend, path, config} for internal use.
77
8+ defmodule S3Config do
9+ @ moduledoc false
10+ @ derive { Inspect , only: [ :bucket , :region , :endpoint ] }
11+ defstruct [
12+ :access_key_id ,
13+ :secret_access_key ,
14+ :region ,
15+ :endpoint ,
16+ :bucket ,
17+ :token
18+ ]
19+
20+ @ type t :: % __MODULE__ {
21+ access_key_id: String . t ( ) ,
22+ secret_access_key: String . t ( ) ,
23+ region: String . t ( ) ,
24+ endpoint: String . t ( ) ,
25+ bucket: String . t ( ) | nil ,
26+ token: String . t ( ) | nil
27+ }
28+ end
29+
830 @ doc """
931 Parses an S3 URL in the format `s3://bucket/key` and returns a triplet.
1032
1133 ## Options
1234
13- * `:config` - A map with S3 configuration keys, or `nil` to read from env vars:
35+ * `:config` - A map/keyword list with S3 configuration keys, or `nil` to read from env vars:
1436 - `AWS_ACCESS_KEY_ID`
1537 - `AWS_SECRET_ACCESS_KEY`
1638 - `AWS_REGION` or `AWS_DEFAULT_REGION`
1739 - `AWS_SESSION_TOKEN`
1840
1941 ## Returns
2042
21- `{:ok, {:s3, key, config}} ` where config is a map with:
43+ `{:ok, {:s3, key, %S3Config{}}} ` where config is a struct with:
2244 - `:access_key_id`
2345 - `:secret_access_key`
2446 - `:endpoint`
@@ -27,7 +49,7 @@ defmodule Explorer.FSS do
2749 - `:token` (optional)
2850 """
2951 @ spec parse_s3 ( String . t ( ) , Keyword . t ( ) ) ::
30- { :ok , { :s3 , String . t ( ) , map ( ) } } | { :error , Exception . t ( ) }
52+ { :ok , { :s3 , String . t ( ) , S3Config . t ( ) } } | { :error , Exception . t ( ) }
3153 def parse_s3 ( url , opts \\ [ ] ) do
3254 opts = Keyword . validate! ( opts , config: nil )
3355
@@ -84,12 +106,14 @@ defmodule Explorer.FSS do
84106
85107 defp normalize_s3_config! ( nil ) , do: s3_config_from_env ( )
86108
109+ defp normalize_s3_config! ( % S3Config { } = config ) , do: config
110+
87111 defp normalize_s3_config! ( config ) when is_map ( config ) do
88- Map . merge ( s3_config_from_env ( ) , config )
112+ struct! ( s3_config_from_env ( ) , config )
89113 end
90114
91115 defp normalize_s3_config! ( config ) when is_list ( config ) do
92- Map . merge ( s3_config_from_env ( ) , Map . new ( config ) )
116+ struct! ( s3_config_from_env ( ) , config )
93117 end
94118
95119 defp normalize_s3_config! ( other ) do
@@ -105,7 +129,7 @@ defmodule Explorer.FSS do
105129 end
106130
107131 defp s3_config_from_env do
108- % {
132+ % S3Config {
109133 access_key_id: System . get_env ( "AWS_ACCESS_KEY_ID" ) ,
110134 secret_access_key: System . get_env ( "AWS_SECRET_ACCESS_KEY" ) ,
111135 region: System . get_env ( "AWS_REGION" , System . get_env ( "AWS_DEFAULT_REGION" ) ) ,
0 commit comments