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.
Prerequisites:
1. Create a new Elixir project with the command:
mix phx.new file_upload_app
cd file_upload_app
2. Next, create a new database with the command:
mix ecto.create
3. Then, add the Ex.Aws.S3 package to your Elixir list of dependencies in your project’s mix.esc file:
7. Then, source this environmental variable file with the command:
source .env
8. Next, in the lib/file_upload_app_web/router.ex file, add the following code:
scope “/”, FileUploadAppWeb do
pipe_through :browser
resources “/upload”, UploadController, only: [:create, :new]
end
9. Create a new migration with the command:
mix ecto.gen.migration add_uploads
10. This command will create a new file located at priv/repo/migrations/add_uploads.exs. Open this file and insert the following code:
defmodule FileUploadApp.Repo.Migrations.AddUploads do
use Ecto.Migration
def change do
create table(:uploads) do
add :image_url, :string
timestamps()
end
end
end
11. Then apply the changes to the Elixir project with the command:
mix ecto.migrate
12. Within the file_upload_app_web folder, create a new folder called models, with a new file inside called upload.ex. Insert the following code:
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
13. Next, within the templates folder, create a new folder called upload, then within that folder create a new file called new.html.eex. Inside this new file, insert the following: