An opinionated file uploader for Elixir projects.
Upload offers the following features:
- Minimal API
- Reasonable defaults
- Ecto integration
- Multiple storage adapters
The package can be installed by adding upload
to your list of dependencies in mix.exs
:
def deps do
[
{:upload, "~> 0.3.0"}
]
end
Upload a file:
{:ok, upload} = Upload.cast_path("/path/to/file.txt")
Transfer the upload to storage:
{:ok, upload} = Upload.transfer(upload)
Get the URL for the upload:
url = Upload.get_url(upload)
Get a signed URL for the upload:
{:ok, signed_url} = Upload.get_signed_url(upload)
Add a column to store a logo:
def change do
alter table(:companies) do
add :logo, :string
end
end
Add a field to your schema:
schema "companies" do
field :logo, :string
end
Cast the upload in your changeset:
def changeset(company, attrs \\ %{}) do
company
|> cast(attrs, [])
|> Upload.Ecto.cast_upload(:logo, prefix: ["logos"])
end
Upload in the controller:
def create(conn, %{"logo" => logo}) do
changeset = Company.changeset(%Company{}, %{"logo" => logo})
case Repo.insert(changeset) do
{:ok, company} ->
# Insert succeeded. Now, you can get the URL:
Upload.get_url(company.logo)
{:error, changeset} ->
# You know the drill.
end
end
In order to serve the files, you'll need to setup Plug.Static
.
If you're using Phoenix, you can add this line to endpoint.ex
:
plug Plug.Static, at: "/", from: :your_app, gzip: false, only: ~w(uploads)
For now, there are four adapters:
Upload.Adapters.Local
- Save files to your local filesystem.Upload.Adapters.S3
- Save files to Amazon S3.Upload.Adapters.Fake
- Don't actually save the files at all.Upload.Adapters.Test
- Keep uploaded files in state, so that you can assert.
Out of the box, Upload
is ready to go with some sane defaults (for development, at least).
Here are the default values:
config :upload, Upload,
adapter: Upload.Adapters.Local
config :upload, Upload.Adapters.Local,
storage_path: "priv/static/uploads",
public_path: "/uploads"
To use the AWS adapter, you'll to install ExAws.
Then, you'll need to following configuration:
config :upload, Upload, adapter: Upload.Adapters.S3
config :upload, Upload.Adapters.S3, bucket: "your_bucket_name"
To use this adapter, you'll need to the following configuration:
config :upload, Upload, adapter: Upload.Adapters.Test
In your tests, you can make assertions:
test "files are uploaded" do
assert {:ok, _} = start_supervised(Upload.Adapters.Test)
assert {:ok, upload} = Upload.cast_path("/path/to/file.txt")
assert {:ok, upload} = Upload.transfer(upload)
assert map_size(Upload.Adapters.Test.get_uploads()) == 1
end
This adapter does pretty much nothing. It makes absolutely no attempt to persist uploads. This can be useful in unit tests where you want to completely bypass uploading.
To use this adapter, you'll need the following configuration:
config :upload, Upload, adapter: Upload.Adapters.Fake
First, install the dependencies:
$ mix deps.get
The tests depend on a "fake" Amazon S3 running locally. If you have Docker installed, you can run:
$ bin/fake-s3
Then, you can run the test suite:
$ mix test