Suggestions for Image Upload in JSON API

Hi, I’m building a REST API using Phoenix, We have a product schema that is related to many images.

We first started thinking more about image uploading while developing the product APIs, we firstly wanted to do a basic multipart upload along with the product data, with leveraging Waffle and ecto to upload to an S3 compatible storage.

However, we were a bit influenced by Phoenix’s LiveView way of handling uploads. Briefly, it uploads files to a tmp location before submitting the other data in the form. It leverages a GenServer that deletes those files if not consumed by the LiveView. This way however, also leverages that the LiveView is an active socket, and all the data needed about files, locations can be communicated through the socket.

And finally, my question is, do you guys recommend replicating this method of uploading images in a JSON API Phoenix app? Given that the primary consumer of this API is a mobile app, and if done, we’ll be mostly be leveraging long polling to maintain a connection with the server and speaking with the GenServer that’ll delete our temp files if not consumed.

If you are uploading to S3, I would recommend uploading directly there in the client, and then having the client send just the metadata to your API. On the server, you can handle “promoting” the file on S3 as part of a scheduled/async task.

1 Like