
Summary
Key points of this document
- Where are the design challenges?
- How to define boundaries?
- Effective implementation details
Overview
Uploading large files via an application server increases server load. While signed URLs allow direct uploads, you need to design the finalization process and permission management. Signed URL uploads are a convenient way to reduce server load, but it's risky if you don't design state management for reservation, validation, and finalization.
Where are the design challenges?
Direct uploads are efficient, but issuing a signed URL allows any client with that URL to upload for a certain period. If you don't restrict the destination or file conditions, unintended objects might be placed.
Uploads from the browser can be interrupted. Registration in the application DB might fail even after a successful upload. A design that can later recover consistency between the DB and GCS is necessary.
Directly uploading to GCS from a browser is suitable for large files as it bypasses the application server. However, if the server treats it as complete without inspecting the actual file content, it might miss unauthorized uploads or invalid formats.
How to define boundaries?
Create an upload plan via API, verify user permissions, filename, size, and MIME type, then return a signed URL. The client uploads directly to GCS and notifies the API of completion for finalization.
In the design, we separate upload reservation, signed URL issuance, client upload, completion notification, validation, and availability. The signed URL is linked to a file ID and user, with limited expiration and allowed methods.
Effective implementation details
Create a temporary file record in the DB and manage its state aspending,uploaded,confirmed,expiredObject keys are generated on the server side, not freely entered by the client. GCS metadata is checked upon finalization.
On the Cloud Run side, a storage_key is generated upon reservation, and a GCS signed URL is issued. After completion notification, GCS metadata is retrieved, and size, content-type, and generation are matched with the DB before transitioning to 'available'.
- Do not use the original filename as the object key; save with an unpredictable ID.
- Do not trust completion notifications; verify the actual entity and metadata on GCS from the server side.
- Prepare a job to clean up expired or partially failed reservation records.
Observe failure modes
During validation, check for expired URLs, MIME type mismatches, finalization failures after upload, abandoned temporary records, and finalization by unauthorized users. Also observe interruptions with large files or mobile connections.
During validation, check for expired URLs, URL usage by different users, size overruns, MIME spoofing, and files without completion notifications. Using GCS generation, also ensure that unexpected overwrites of the same key do not occur.
Discarded options and tradeoffs
Direct uploads reduce server load but complicate the flow. For small files only, uploading via the application server is simpler. Decide on adoption based on file size and frequency.
Direct uploads are easier to scale but increase state management. For small files only, uploading via the application might be simpler. The method should be chosen based on file size, security, and implementation cost.
Decision criteria for the field
GCS signed URLs are a tool to streamline the upload path. By designing to include temporary registration, finalization, and cleanup of unfinalized items, discrepancies between the DB and storage can be minimized.
Signed URLs are a mechanism to open up an upload path. To use them safely, you need to design validation of both the DB state before and after URL issuance and the actual GCS entity as a set.


