File Service Architecture Graph
The File service is responsible for handling file uploads, file downloads, resizing images, and file delete events.
The File Manager app is a good example of an app that relies on the File Service.
- A - CloudFront web distribution
- B - APIGateway
- C - Apollo Gateway service
- D - File service
- E - S3 file storage
- F - File download service
- G - Image resize service
- H - "Manage files" Lambda function
A) File upload
Client makes a POST request with the file metadata to the API.
The client never sends the full file body to the GraphQL endpoint as a Lambda function can't handle large files.
The request goes through the (A) CloudFront to the (B) ApiGateway and then hits the (C) Apollo Gateway where the (D) File service takes over the request.
The (D) File service uses the file metadata to generate the pre-signed POST data.
To learn in detail more about this whole process, checkout our blog post on how to Upload files to AWS S3 using pre-signed POST data and a Lambda function.
The (C) File service responds with the pre-signed POST data.
Finally, the Client makes a POST request to the (E) S3 file storage, which includes the received pre-signed POST data, and the actual file that needs to be uploaded.
This way the client uploads files directly to S3, while still keeping the security around who can upload what types of files. This approach also enables you to turn on S3 Transfer Acceleration feature for faster upload.
B) File download
Client requests a file that was previously uploaded.
The request goes through the (A) CloudFront, and if it's not found in the CDN cache, the request goes to the (B) ApiGateway and then hits the (F) File download service.
- (F) File download service is a special lightweight function for serving files back to the client.
If the file is larger than 6MB, the (F) File download service will return a 301 redirect with a path to the object on S3 bucket and it will make the object public. This is to get around the Lambda invocation payload limits.
C) Request for an image
If the request is for an image for a specific image size, the (F) File download service will pass on the request to (G) Image resize service.
(G) Image resize service resizes the image and saves the specific image to the (E) S3 file storage.
- To protect agains brute-force attacks, Webiny has a fixed set of image sizes.
- In case you request an image of 282px, Webiny will return the first greater image size.
The image is returned to the client, by passing it to the (A) CloudFront CDN, where it's also cached.
D) File deleted
For file deletions, the delete action is first issued as a GraphQL mutation, which is processed by the (F) File download service.
The (F) File download service only deletes the original file from the S3 bucket, even if the file is an image, in which case multiple image sizes still might exist.
The deletion of the original file will emit an event, on which the (H) "Manage files" Lambda function will be invoked. In case the originally deleted file was an image, it will commence the deletion of the additional image sizes that might've been requested over the course of the file's existence.