Kathan
Kathan

Reputation: 1458

Rails app and Android app connected to s3 bucket

I have a rails 4 app that uses carrierwave and Amazon s3 for image uploading. I also have a rest api built for my mobile application to talk to the database.

What is the best way to handle image storing through the app (so that they website and app stay congruent)?

Right now, I am contemplating one of two options.

  1. The android app sends the image directly to the same amazon s3 bucket that the website uses, and then sends a post request to the rails app with the image url to be stored.
  2. The android app sends the entire image through a post request and the rails app handles everything else (storing the path and sending the image to the amazon bucket).

Would love some feedback regarding the best way to do this - or if there is another way entirely. Still new to apis and having a web app and mobile app communicate. If you already know of documentation on this subject, please let me know. thnx!

Upvotes: 2

Views: 176

Answers (2)

Eric Platon
Eric Platon

Reputation: 10152

The second option is to me the easier bet.

The first option relies on mobile devices to preserve consistency of data, which is very hard in practice---well, perhaps "impossible", at least unreliable. A mobile device loses connectivity at arbitrary timings, and when it happens between the S3 upload and the API call, consistency is lost. This consistency may not matter much for the target application (it depends what you do with the API), yet it is often an essential property, just to know what the system state is, and when debugging. Delegating the S3 storage to the API moves the consistency problem to a single point, where communications are way more reliable.

The second option offers extra advantages. Going through your "proprietary" API, it does not matter what actual storage you are using. Today is S3, but tomorrow's pricing may lead you to another backend. Behind an API that abstracts away the storage detail, you keep the freedom to change whenever you want or have to.

Another advantage is security. The API becomes the only trusted party with write access. All mobile devices just have read-only rights. Way easier to manage and to guarantee some level of security. Let alone safety issues, where a bug in the code could have some device overwrite someone else's images.

Analytics can also be more comprehensive or fine-grained going through an API.

I believe there are some other benefits that I do not recall yet, and some issues too, like a potential bottleneck / single point of failure. Overall the second option is to me the best choice for the conditions you presented.

Upvotes: 3

ggtmtmgg
ggtmtmgg

Reputation: 147

I recommend you first option. There are two reasons:

  1. Carrierwave uploades images and saves model to database at same time. It means if you upload images without through carrierwave, carrierwave will upload images again when model is saved.

  2. Carrierwave will name image as follow your settings of carrierwave when image is uploaded. I can imagine you'll set up random name to keep unique. So you need to through carrierwave to get images name.

So I believe if you choose another option, it'll be hard.

Upvotes: 1

Related Questions