In a real-estate listing portal's web uploader, what steps does the page take to send one photo straight to storage with a pre-signed URL?
answer
- three requests, two origins
- ask the portal's API, not the store
- the bytes cross origins without a cookie
- tell the API when the bytes land
- the URL is a short-lived credential
basics
~20 sThe page asks the portal's API for an upload id and a short-lived pre-signed URL, sends the photo's bytes straight to that URL, then tells the API the upload finished so the server can verify and attach it.
solid answer
~50 sPer photo the page runs three steps. First it asks the portal's own API for permission, sending the listing id and the file's name, size and type; the API answers with an upload id and a pre-signed URL that expires in minutes. Second it sends the `File` itself as the request body to that URL. That is another origin, so the storage side must allow the portal's origin through CORS, and no session cookie rides along: the URL *is* the authority. Third it calls the API with the upload id to say the bytes landed; the server checks the stored object and only then counts it toward publishing the listing. Going direct keeps a 3 GB walkthrough video off the API's servers and out of their body-size and timeout limits. If storage answers 403 after retries or a slow send, the URL has probably expired: ask for a fresh one instead of failing the photo.
code
http · 26 linesPOST /api/listings/88214/uploads HTTP/1.1
Host: portal.example
Content-Type: application/json
{"name":"living-room-01.jpg","size":912384,"type":"image/jpeg"}
HTTP/1.1 201 Created
Content-Type: application/json
{"uploadId":"u-7f3a","url":"https://storage.example/l-88214/u-7f3a?sig=...&expires=...","expiresAt":"2026-10-07T09:20:00Z"}
PUT /l-88214/u-7f3a?sig=...&expires=... HTTP/1.1
Host: storage.example
Origin: https://portal.example
Content-Type: image/jpeg
Content-Length: 912384
<the photo's bytes>
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://portal.example
POST /api/listings/88214/uploads/u-7f3a/complete HTTP/1.1
Host: portal.example
HTTP/1.1 200 OKgo deeper
Recall the three steps: ask the portal's API, send the bytes to the storage URL, report completion. Remember that the URL itself, not a cookie, authorises the write.
Explain what changes when the page writes to another origin: CORS and its preflight on the storage side, no cookies, and raw storage errors the page must turn into messages an agent understands.
Show the failure path of each step: URLs fetched just in time so none expire in the queue, a fresh URL after an expiry 403, and a completion call that is safe to repeat.
Weigh the URL's lifetime from the client's seat: a short one limits a leaked link, a long one spares slow uploads a refresh. Whatever the issuer picks, the page must refresh gracefully.
## The shape: three requests, two origins A **pre-signed URL** is a storage address with a signature and an expiry baked into it: whoever holds it may perform one narrow operation, such as writing one object, until it expires. In a real-estate listing portal, an agent drops 40 to 80 photos and a walkthrough video onto the listing page. For each photo the page runs the same three steps: 1. **Ask for authority.** The page calls the portal's own API (same origin, the agent's session cookie attached) with the listing id and the file's `name`, `size` and `type`. The API answers with an **upload id** and a pre-signed URL that is valid for a few minutes. 2. **Send the bytes.** The page sends the `File` object itself as the body of a request to that URL. Progress for the bar comes from `XMLHttpRequest`'s `upload` object, which fires progress events while the body is sent. 3. **Report completion.** The page calls the portal's API again with the upload id. The server checks the stored object and attaches it to the listing. The listing can be published only once the server has confirmed every upload; that gate lives on the server, not in the page. ## Why the bytes skip the portal's API Seen from the browser, the reasons are concrete: - A 3 GB video posted to the API would hit the API's own request-size and timeout limits long before a weak phone uplink finished it. - Every byte would cross the network twice (browser to API, API to storage), and a slow phone would hold an API request open for an hour. - The page's other calls to the API stay fast because the heavy traffic goes to a different host. Why the server side prefers this pattern, and how it issues and scopes the URL, belongs to the questions on server ingest and on the valet-key pattern; the page only has to use what it is given. ## What changes for the page when it talks to storage | Concern | Posting to the portal's API | Sending to a pre-signed URL | |---|---|---| | Who authorises the write | the session cookie | the signature in the URL itself | | Origin | same origin | another origin: storage must allow the portal's origin through CORS | | Preflight | usually none | yes for a `PUT`, and attaching upload listeners also forces one | | Error vocabulary | the API's error envelope | the storage service's raw status codes, which the page must translate | | Knowing it is done | the API's response | an explicit completion call to the API | The preflight row matters in practice: if the storage side is not configured to answer the `OPTIONS` request for the portal's origin, every upload fails before a byte is sent, and the page sees only a network error. ## Failures at each step - **Asking for the URL fails.** A network error or a 5xx is retried with backoff; a 4xx such as "this listing already has 80 photos" is shown to the agent as a reason. - **Sending fails with a network error.** Retry the same URL if it is still inside its expiry. - **Storage answers 403 after retries or a slow send.** The likely cause is an expired URL. Ask the API for a fresh URL for the same upload id and retry once; if the fresh URL also fails, stop the file and say so. - **The completion call fails.** The bytes are already stored, so never resend them. Retry the completion call with the same upload id, which the API must treat as safe to repeat, and show the photo as "finishing" rather than failed. ## Handle the URL like a credential The URL authorises a write to the portal's storage, so the page treats it like a password with a short life: - Request it just before the photo is sent, not for all 80 photos when they are dropped in; on a weak uplink the last ones would wait long enough for their URLs to expire. - Keep it in memory only; never write it to `localStorage` or IndexedDB, where it outlives the page. - Strip it from error reports, analytics events and console logs, which travel to places the agent never sees. A candidate who can walk these three steps, name the CORS and cookie consequences, and say what happens when each step fails has the whole flow that later questions about progress, resume and concurrency build on.
- Why not request all eighty pre-signed URLs as soon as the agent drops the photos in?Because they expire. On a weak phone uplink the last photos may wait longer than the URL's lifetime, so the page would meet a wall of 403s halfway through. Ask for each URL just before its photo is sent; fetching URLs for the next few queued files in one call is a fair middle ground that saves round trips without letting URLs go stale.
- Storage accepted the photo, but the completion call to the portal's API timed out. What does the page do?Retry the completion call with the same upload id and keep the row in a "finishing" state. The bytes are already stored, so re-uploading would waste the agent's uplink and might create a duplicate. The API must accept a repeated completion for the same id without harm; if retries keep failing, show the photo as not yet confirmed so the agent knows the listing cannot publish yet.
- Stream the 3 GB walkthrough video through the portal's own API like any form post
- The storage request is authorised by the portal's session cookie
- A 200 from storage means the photo is already part of the listing
- Fetch every file's pre-signed URL up front when the agent picks the photos
- A 403 from storage mid-queue means the agent lacks permission, so fail the file
- Log the full pre-signed URL in error reports to make debugging easier