Skip to content

Commit f0ca019

Browse files
committed
add s3
1 parent dcea377 commit f0ca019

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ GITHUB_PRIVATE_KEY=""
1212
GITHUB_PAT=""
1313
GITHUB_PAT_ENABLED=false
1414

15+
AWS_ENDPOINT_URL_S3=""
16+
AWS_REGION=""
17+
AWS_ACCESS_KEY_ID=""
18+
AWS_SECRET_ACCESS_KEY=""
19+
BUCKET_NAME=""
20+
1521
STRIPE_PUBLISHABLE_KEY=""
1622
STRIPE_SECRET_KEY=""
1723
STRIPE_WEBHOOK_SECRET=""

config/dev.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ config :algora, Algora.Repo,
3030
migration_primary_key: [type: :string],
3131
migration_timestamps: [type: :utc_datetime_usec]
3232

33+
config :ex_aws,
34+
json_codec: Jason,
35+
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),
36+
secret_access_key: System.get_env("AWS_SECRET_ACCESS_KEY")
37+
38+
config :ex_aws, :s3,
39+
scheme: "https://",
40+
host: if(url = System.get_env("AWS_ENDPOINT_URL_S3"), do: URI.parse(url).host),
41+
region: System.get_env("AWS_REGION")
42+
3343
# For development, we disable any cache and enable
3444
# debugging and code reloading.
3545
#
@@ -108,6 +118,7 @@ config :swoosh, :api_client, false
108118
config :salad_ui, components_path: Path.join(File.cwd!(), "lib/algora_web/components/ui")
109119

110120
config :algora,
121+
bucket_name: System.get_env("BUCKET_NAME"),
111122
cloudflare_tunnel: System.get_env("CLOUDFLARE_TUNNEL"),
112123
swift_mode: System.get_env("SWIFT_MODE") == "true",
113124
auto_start_pollers: System.get_env("AUTO_START_POLLERS") == "true"

config/runtime.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ if config_env() == :prod do
5656
migration_primary_key: [type: :string],
5757
migration_timestamps: [type: :utc_datetime_usec]
5858

59+
config :ex_aws,
60+
json_codec: Jason,
61+
access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"),
62+
secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY")
63+
64+
config :ex_aws, :s3,
65+
scheme: "https://",
66+
host: URI.parse(System.fetch_env!("AWS_ENDPOINT_URL_S3")).host,
67+
region: System.fetch_env!("AWS_REGION")
68+
5969
# The secret key base is used to sign/encrypt cookies and other secrets.
6070
# A default value is used in config/dev.exs and config/test.exs but you
6171
# want to use a different value for prod and you most likely don't want
@@ -136,5 +146,6 @@ if config_env() == :prod do
136146
# See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details.
137147

138148
config :algora,
149+
bucket_name: System.fetch_env!("BUCKET_NAME"),
139150
auto_start_pollers: System.get_env("AUTO_START_POLLERS") == "true"
140151
end

lib/algora/s3.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Algora.S3 do
2+
@moduledoc false
3+
4+
def endpoint_url do
5+
config = Application.fetch_env!(:ex_aws, :s3)
6+
"#{config[:scheme]}#{config[:host]}"
7+
end
8+
9+
def bucket_name, do: Algora.config([:bucket_name])
10+
11+
def bucket_url, do: endpoint_url() <> "/" <> bucket_name()
12+
13+
def upload(body, object, opts \\ []) do
14+
bucket_name()
15+
|> ExAws.S3.put_object(object, body, opts)
16+
|> ExAws.request([])
17+
end
18+
end

mix.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ defmodule Algora.MixProject do
7575
{:oban, "~> 2.17"},
7676
{:styler, "~> 1.2", only: [:dev, :test], runtime: false},
7777
{:typed_ecto_schema, "~> 0.4.1", runtime: false},
78-
{:chameleon, "~> 2.2.0"}
78+
{:chameleon, "~> 2.2.0"},
79+
# ex_aws
80+
{:ex_aws, "~> 2.1"},
81+
{:ex_aws_s3, "~> 2.0"},
82+
{:hackney, "~> 1.9"}
7983
]
8084
end
8185

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"ecto": {:hex, :ecto, "3.12.5", "4a312960ce612e17337e7cefcf9be45b95a3be6b36b6f94dfb3d8c361d631866", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6eb18e80bef8bb57e17f5a7f068a1719fbda384d40fc37acb8eb8aeca493b6ea"},
1616
"ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"},
1717
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
18+
"ex_aws": {:hex, :ex_aws, "2.5.8", "0393cfbc5e4a9e7017845451a015d836a670397100aa4c86901980e2a2c5f7d4", [:mix], [{:configparser_ex, "~> 4.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:req, "~> 0.3", [hex: :req, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8f79777b7932168956c8cc3a6db41f5783aa816eb50de356aed3165a71e5f8c3"},
19+
"ex_aws_s3": {:hex, :ex_aws_s3, "2.5.6", "d135983bbd8b6df6350dfd83999437725527c1bea151e5055760bfc9b2d17c20", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "9874e12847e469ca2f13a5689be04e546c16f63caf6380870b7f25bf7cb98875"},
1820
"ex_cldr": {:hex, :ex_cldr, "2.40.2", "1361d06624d7533fb3a2df59c82c7108b18ef55e884f48d503597fce4ce28d97", [:mix], [{:cldr_utils, "~> 2.28", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "cd9039ca9a7c61b99c053a16bd2201ebd7d1c87b49499a4c6d761ec14bca4442"},
1921
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.16.3", "1ec6444b5d0c0aabba5a3bc321d73f1c9c751c6add92e7fb7775ccc071d96bd8", [:mix], [{:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4d1b5f8449fdf0ece6a2e5c7401ad8fcfde77ee6ea480bddc16e266dfa2b570c"},
2022
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.33.4", "ecb06f40fc63f484a53d4ea80e1bdd6860ec44d3032f2b10b17340d34c0a13d5", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.38", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.16", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "d15b7e217e9e60c328e73045e51dc67d7ac5d2997247b833efab2c69b2ed06f5"},

0 commit comments

Comments
 (0)