upload
An opinionated file uploader for Elixir projects.
Upload offers the following features:
- Minimal API
- Reasonable defaults
- Ecto integration
- Multiple storage adapters
Installation
The package can be installed by adding upload to your list of dependencies in mix.exs:
[
{:upload, "~> 0.3.0"}
]
end
Usage
Upload a file:
Transfer the upload to storage:
Get the URL for the upload:
Get a signed URL for the upload:
Ecto Integration
Add a column to store a logo:
alter table(:companies) do
add :logo, :string
end
end
Add a field to your schema:
field :logo, :string
end
Cast the upload in your changeset:
company
|> cast(attrs, [])
|> Upload.Ecto.cast_upload(:logo, prefix: ["logos"])
end
Upload in the controller:
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
Serving static files
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:
Configuration
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.
Upload.Adapters.Local
Out of the box, Upload is ready to go with some sane defaults (for development, at least).
Here are the default values:
adapter: Upload.Adapters.Local
config :upload, Upload.Adapters.Local,
storage_path: "priv/static/uploads",
public_path: "/uploads"
Upload.Adapters.S3
To use the AWS adapter, you'll to install ExAws.
Then, you'll need to following configuration:
config :upload, Upload.Adapters.S3, bucket: "your_bucket_name"
Upload.Adapters.Test
To use this adapter, you'll need to the following configuration:
In your tests, you can make assertions:
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
Upload.Adapters.Fake
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:
Contributing
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