Skip to content

Commit

Permalink
Move :bucket from S3 entry to config
Browse files Browse the repository at this point in the history
  • Loading branch information
philss committed Aug 17, 2023
1 parent 417a1b6 commit 90dd8f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
39 changes: 21 additions & 18 deletions lib/fss/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule FSS.S3 do

defstruct [
:access_key_id,
:bucket,
:region,
:secret_access_key,
:endpoint,
Expand All @@ -22,6 +23,7 @@ defmodule FSS.S3 do
The attributes are:
* `:access_key_id` - This attribute is required.
* `:bucket` - A valid bucket name. This attribute is required.
* `:region` - This attribute is required.
* `:secret_access_key` - This attribute is required.
* `:endpoint` - This attribute is optional. If specified, then `:region` is ignored.
Expand All @@ -31,6 +33,7 @@ defmodule FSS.S3 do
"""
@type t :: %__MODULE__{
access_key_id: String.t(),
bucket: String.t(),
region: String.t(),
secret_access_key: String.t(),
endpoint: String.t() | nil,
Expand All @@ -43,19 +46,17 @@ defmodule FSS.S3 do
Represents the S3 resource itself.
"""

defstruct [:bucket, :key, :config]
defstruct [:key, :config]

@typedoc """
The entry struct for S3.
The attributes are:
* `:bucket` - A valid bucket name. This attribute is required.
* `:key` - A valid key for the resource. This attribute is required.
* `:config` - A valid S3 config from the type `Config.t()`. This attribute is required.
"""
@type t :: %__MODULE__{
bucket: String.t(),
key: String.t(),
config: Config.t()
}
Expand All @@ -77,6 +78,9 @@ defmodule FSS.S3 do
- `AWS_SECRET_ACCESS_KEY`
- `AWS_REGION` or `AWS_DEFAULT_REGION`
- `AWS_SESSION_TOKEN`
Although you can pass the `:bucket` as a configuration option, it's not
possible to override the bucket name from the URL.
"""
@spec parse(String.t(), Keyword.t()) :: {:ok, Entry.t()} | {:error, Exception.t()}
def parse(url, opts \\ []) do
Expand All @@ -89,23 +93,10 @@ defmodule FSS.S3 do
config =
opts
|> Keyword.fetch!(:config)
|> case do
nil ->
config_from_system_env()

%Config{} = config ->
config

config when is_list(config) or is_map(config) ->
struct!(config_from_system_env(), config)

other ->
raise ArgumentError,
"expect configuration to be a %FSS.S3.Config{} struct, a keyword list or a map. Instead got #{inspect(other)}"
end
|> normalize_config!()
|> validate_config!()

{:ok, %Entry{bucket: bucket, key: key, config: config}}
{:ok, %Entry{key: key, config: %{config | bucket: bucket}}}

_ ->
{:error,
Expand All @@ -124,6 +115,18 @@ defmodule FSS.S3 do
config
end

defp normalize_config!(nil), do: config_from_system_env()
defp normalize_config!(%Config{} = config), do: config

defp normalize_config!(config) when is_list(config) or is_map(config) do
struct!(config_from_system_env(), config)
end

defp normalize_config!(other) do
raise ArgumentError,
"expect configuration to be a %FSS.S3.Config{} struct, a keyword list or a map. Instead got #{inspect(other)}"
end

defp check!(config, key, env) do
if Map.fetch!(config, key) in ["", nil] do
raise ArgumentError,
Expand Down
7 changes: 6 additions & 1 deletion test/fss/s3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ defmodule FSS.S3Test do
end

test "parses a s3:// style uri", %{config: config} do
assert {:ok, %Entry{bucket: "my-bucket", key: "my-file.png", config: %Config{} = config}} =
assert {:ok, %Entry{key: "my-file.png", config: %Config{} = config}} =
S3.parse("s3://my-bucket/my-file.png", config: config)

assert is_nil(config.endpoint)
assert config.bucket == "my-bucket"
assert config.secret_access_key == "my-secret"
assert config.access_key_id == "my-access"
assert config.region == "us-west-2"
Expand All @@ -38,6 +39,7 @@ defmodule FSS.S3Test do
)

assert config.endpoint == "localhost"
assert config.bucket == "my-bucket"
assert config.secret_access_key == "my-secret-1"
assert config.access_key_id == "my-access-key-1"
assert config.region == "eu-east-1"
Expand All @@ -50,11 +52,14 @@ defmodule FSS.S3Test do
endpoint: "localhost",
secret_access_key: "my-secret-1",
access_key_id: "my-access-key-1",
# We always ignore bucket from config.
bucket: "random-name",
region: "eu-east-1"
}
)

assert config.endpoint == "localhost"
assert config.bucket == "my-bucket"
assert config.secret_access_key == "my-secret-1"
assert config.access_key_id == "my-access-key-1"
assert config.region == "eu-east-1"
Expand Down

0 comments on commit 90dd8f4

Please sign in to comment.