How can I handle File Uploads in a Microservice Environment? How can I handle File Uploads in a Microservice Environment? php php

How can I handle File Uploads in a Microservice Environment?


I think to those architectural questions there is no one-fits-all solution, it always depends on your context and quality goals.

If you prefer the encapsulation over performance, then go with solution (2). You might want to consider using a client-based service discovery mechanism for the file service instead of a full-blown API-Gateway to reduce the load on the gateway.

If you prefer performance and the client is under your control, then you can go with solution (3).

I'd avoid solution (1), though. Microservices have a principle "keep the smarts in the endpoints", meaning to avoid putting logic into infrastructure components like API-Gateways.


[CLIENT:file] ->     [FILE API]     -> {response}     ->         [a: MICROSERVICE-News]        [b: MICROSERVICE-Authors]        [c: MICROSERVICE-Logger]    ] -> {publish}

You can skip Gateway api in this example.

You File api verifies the file upload, and the data content. After it is succesfull, you make a response to a client that the upload was succesfull.

Later you process the upload in your microservices, and you use a sort of publisher to notify the client if for this data adds new actions in your other services.

In case your client depends on the processing of data, you have then 2 steps. First step is file upload response, that tells you that file was succesfully uploaded and verified.

Here you can react to this information by changing the gui, or still wait for the publisher information.Publisher will send the imlpementation of the file information with either success or error, so you can finish your steps with your client


After a long research, I finally got a solution.

Scenario 3 is appropriate for you and you can skip the Gateway api as it says in @Tree

[CLIENT:file] ->     [FILE API] -> {response} -> [CLIENT] ->     [GATEWAY API        [a: MICROSERVICE-News]        [b: MICROSERVICE-Authors]        [c: MICROSERVICE-Logger]    ] -> {response}

Example Scenario

  • user requests file upload

Sample Request

POST https://api.site.com/media/upload

Sample Response

{    "location": "JPG-IMG-54f022ae8b3f4d979e925b4dp68e87"}
  • The user sends the request to share the news

Sample Request

POST https://api.site.com/v2/news{    "Text": "Foo and Bar",    "location": "JPG-IMG-54f022ae8b3f4d979e925b4dp68e87"}
  • Uploaded files are controlled by api gateway

Sample Request

Get https://api.site.com/v2/JPG-IMG-54f022ae8b3f4d979e925b4dp68e87
  • After verification, each microservice performs its own task