Reputation: 1724
So, I'm building an android app which uses a RESTful API as it's backend. The intention to to use the API as a datasource for a future website as well, and possibly an IOS app.
The api is built on Spring Boot and running on Heroku.
I'm trying to figure the proper way to serve images from google cloud storage for my app, but I'm confused.
I hava database table for user entities with columns such as email, name, etc., and one of these columns is supposed to hold a URI for their profile picture.
I'm confused about how to do things from this point on in a proper restful manner. I've boiled my ideas down to this, but there are some problems:
Uploading an image:
User makes a POST request to users/{id}/profilepic
with a .jpg payload. The server then uploads the image (after possible validation/authentication) to google cloud storage, gets the URL for the image there and stores it in the database column.
Retrieving an image:
User makes a GET request to users/{id}/profilepic
. The server looks up the URI for their profile picture in the database, makes it's own request to the URI to retrive the image, and returns the image to the user as .jpg file (encoded how?).
Problem - Middleman inefficiency and authentication/access control - It seems a little redundant (and possibly slow) for the server to be a sort of middle man here, but I do see advantages to it. The alternative would be to send the frontend (app, website, etc.) the URL to the image and allows the frontend to retrieve it itself, but this would mean I have no control over who can access the image.
Is this a good model? How do I go about implementing it in code? Are there any tutorials that cover this?
Upvotes: 2
Views: 2111
Reputation: 924
It's been a long time since you've posted this question, but i'm asking me the same. What was your go to solution. Did you stick with the Man in the middle using the backend to retrive and serve the image or did you find a way to fetch them directly from front end without security issues ?
Thanks for your answer
Upvotes: 0
Reputation: 38379
Your approach seems fine. There are more efficient options, but you'll have to make tradeoffs between things like security, RESTfulness, etc. Below is another approach that has some different pros and cons:
Upload: The client would request from the server permission to upload a profile pic (POST to users/{id}/upload_profilepic). The server would then sign a Google Cloud Storage URL to be used for uploading that pic and hand it to the user, which would then upload the image directly to GCS through a put.
Download: a client would be aware that the GCS resources (which would presumably be public) have a well-known pattern (https://storage.googleapis.com/bucketname/users/{id}/profilepic.jpg) and request them directly.
Upvotes: 1