What is the right way to store files and upload them using the Jason api?

Hello,

I couldn’t find how to deal with files using ash in the docs.

I found the File Type but it’s not what I expect.

I want to be able to store the file on disk and save the path in the database.

In the future I want to upload files using the JSON API.

Thank you!

The :file type is primarily meant for accepting files over interfaces. You’d add a file as an argument, and then use a change on the action to take that file and do something with it. Like you can move it from the temporary directory it is uploaded to by default when it’s a plug.upload, etc.

Really need to write a more comprehensive guide on dealing with file uploads: AshJsonApi.Plug.Parser — ash_json_api v1.4.16

1 Like

Thanks for your quick reply, @zachdaniel !

To be honest, I have no idea how I could do it :slight_smile:

I need to add the :file type as an attribute and another :string attribute to indicate the path or URL where I store the file?

Do you think it’s possible to use Wafle or any other upload library with custom storage options to manage the uploaded files in ash?

What would be the basic steps to implement the possibility to work with files?

I guess this is not integrated with ash_admin yet isn’t it?

Thanks for your help!

Correct on the admin question, but not yet, but we would like to implement that :slight_smile:

You can potentially use Waffle, although not something like WaffleEcto

create :create_user do
  argument :profile_photo, :file
  change fn changeset, _ ->
    Ash.Changeset.before_transaction(changeset, fn changeset, _ ->
      if upload = changeset.arguments[:file] do
         url = <upload to s3>
         Ash.Changeset.force_change_attribute(changeset, :profile_photo_url, url)
      else
         changeset
      end
    end
  end
end
2 Likes