Elixir Phoenix
Learn how to configure an Elixir Phoenix App for use with Filebase.
Elixir Phoenix is a web framework built using the Elixir programming language. It is designed to help developers quickly build scalable and fault-tolerant web applications that can handle large amounts of traffic. Phoenix provides many features that are common to modern web frameworks, including a request/response cycle, a routing system, controllers, views, and templates. It is a popular choice for building real-time applications, such as chat apps, games, and social media platforms, as well as traditional web applications.
Read below to learn how to use Elixir Phoenix with Filebase.
mix phx.new file_upload_app
cd file_upload_app
mix ecto.create
{:ex_aws, "~> 2.0"},
{:ex_aws_s3, "~> 2.0"},
{:poison, "~> 3.0"},
{:hackney, "~> 1.9"},
{:sweet_xml, "~> 0.6.6"},
mix deps.get
config :ex_aws,
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),
secret_access_key: System.get_env("AWS_SECRET_ACCESS_KEY"),
s3: [
scheme: "https://",
host: "bucket-name.s3.filebase.com",
region: "us-east-1"
]
Replace ‘
bucket-name
’ with your Filebase bucket name.export AWS_ACCESS_KEY_ID=FILEBASE_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=FILEBASE_SECRET_KEY
source .env
scope “/”, FileUploadAppWeb do
pipe_through :browser
resources “/upload”, UploadController, only: [:create, :new]
end
mix ecto.gen.migration add_uploads
defmodule FileUploadApp.Repo.Migrations.AddUploads do
use Ecto.Migration
def change do
create table(:uploads) do
add :image_url, :string
timestamps()
end
end
end
mix ecto.migrate
defmodule FileUploadApp.Upload do
use FileUploadApp.Web, :model
schema "uploads" do
field :image_url, :string
timestamps()
end
def changeset(struct, params \\\\ :invalid) do
struct
|> cast(params, [:image_url])
|> validate_required([:image_url])
end
end
<%= form_for @changeset, upload_path(@conn, :create), [multipart: true] fn f -> %>
<div class="form-group">
<%= label f, :image, class: "control-label" %>
<%= file_input f, :image, class: "form-control" %>
<%= error_tag f, :image %>
</div>
<%= submit "Upload Image", class: "btn" %>
<% end %>
defmodule FileUploadApp.UploadController do
use FileUploadApp.Web, :controller
alias FileUploadApp.Upload
def new(conn, _params) do
changeset = Upload.changeset(%Upload{})
render conn, "new.html", changeset: changeset
end
def create(conn, %{"upload" => %{"image" => image_params} = upload_params}) do
file_uuid = UUID.uuid4(:hex)
image_filename = image_params.filename
unique_filename = "#{file_uuid}-#{image_filename}"
{:ok, image_binary} = File.read(image_params.path)
bucket_name = System.get_env("BUCKET_NAME")
image =
ExAws.S3.put_object(bucket_name, unique_filename, image_binary)
|> ExAws.request!
updated_params =
upload_params
|> Map.update(image, image_params, fn _value -> "https://#{bucket_name}.s3.filebase.com/#{unique_filename}" end)
changeset = Upload.changeset(%Upload{}, updated_params)
case Repo.insert!(changeset) do
{:ok, upload} ->
conn
|> put_flash(:info, "Image uploaded successfully!")
|> redirect(to: upload_path(conn, :new))
{:error, changeset} ->
render conn, "new.html", changeset: changeset
end
end
end
mix phoenix.server
If you have any questions, please join our Discord server, or send us an email at [email protected]
Last modified 6mo ago