Your GET /exports/{id}/download handler reads a two-gigabyte export from object storage and
copies it to the client. Each download holds a request handler, a connection to the bucket, and
your server’s bandwidth for as long as the slowest client takes. A deploy that restarts the
process cuts every download in flight.
What you get
You will end up with a download endpoint that authorizes the caller and then sends the bytes from
object storage or nginx, with curl output showing Accept-Ranges, ETag and a 206. This is for
you if your API serves files larger than you want to stream through it.
Short answer
Authorize GET /exports/{id}/download in the API, then answer 303 See Other with a presigned GET
URL for the object, signed with response-content-disposition so the store sends the filename.
The client fetches the bytes from storage, which answers Range with 206. Encode the filename as
RFC 6266 filename* for non-ASCII names, and send a resuming client back through the API for a
fresh URL.
You will need
Node 22 or later, Go 1.23 or later for the proxying comparison, curl, and an object store that
issues presigned URLs. Verified 2026-11-30 against Node 22.23.5, Go 1.24.7, curl 8.5.0,
@aws-sdk/client-s3 3.1180.0, and express 5.3.0. The sample signs URLs offline with the AWS SDK
and made-up credentials. A stand-in bucket on 127.0.0.1 checks them, and the sample’s README.md
lists what it copies from S3 and what it leaves out.
Approaches compared
| Approach | When it fits | What it costs you | When to pick something else |
|---|---|---|---|
| 303 to a presigned GET URL (S3, GCS, or Azure SAS) | The file is already in object storage, and the client follows redirects | Anyone holding the URL can fetch it until expiry, and you cannot revoke one download | You must revoke access mid-download, or log every byte range served |
Go http.ServeContent or Express res.sendFile, proxying | Small files, or you need per-request headers, logging, or revocation | A connection, a handler, and bandwidth on your server for each download’s full length | Files are large and downloads long, which ties up the app |
nginx X-Accel-Redirect or Apache X-Sendfile | nginx already fronts the app and can reach the file | nginx config to keep in step with the app, and nginx still carries every byte | The file lives in object storage that clients can reach directly |
The presigned redirect moves both the bytes and the connection off your servers, at the price of a
URL that works for anyone until it expires. X-Accel-Redirect keeps the URL private and the
authorization in the app, but nginx still serves every byte. A proxying app keeps full control of
headers and access, and spends your app’s connections for the whole of every download.
Authorize, then answer 303 with a presigned URL
startApp in app.mjs checks the bearer token and the export’s owner before it signs anything. An
export that belongs to another account is a 404, so its id reveals nothing.
async function download(req, res, id) {
const account = tokens.get(req.headers.authorization ?? '')
if (!account) return send(res, 401, { error: 'missing or unknown token' })
const record = exports.get(id)
if (!record || record.owner !== account) return send(res, 404, { error: 'no such export' })
const disposition = contentDisposition(record.filename)
if (mode === 'presign') {
const command = new GetObjectCommand({
Bucket: bucket,
Key: record.key,
ResponseContentDisposition: disposition,
ResponseContentType: record.type,
})
const url = await getSignedUrl(s3, command, { expiresIn, signingDate: new Date(now()) })
res.writeHead(303, { location: url, 'cache-control': 'no-store' })
return res.end()
}
303 See Other tells the client to
retrieve the Location with a GET or a HEAD request. getSignedUrl signs offline, so the API makes
no request to the bucket. ResponseContentDisposition
becomes the response-content-disposition query parameter, and S3 returns it as the
Content-Disposition header. Google Cloud Storage takes the same parameter on a
signed URL,
and an Azure SAS takes
rscd.
verify.mjs starts the stand-in bucket, both modes of the API, and the Express proxy, then runs
curl against each route. The signature is printed as its length.
node verify.mjs
303 to a presigned URL: curl -I
HTTP/1.1 303 See Other
location: http://127.0.0.1:35481/exports-bucket/acct_1/exp_42.csv
X-Amz-Algorithm=AWS4-HMAC-SHA256
X-Amz-Content-Sha256=UNSIGNED-PAYLOAD
X-Amz-Credential=demo-key-id/20261130/us-east-1/s3/aws4_request
X-Amz-Date=20261130T120000Z
X-Amz-Expires=300
X-Amz-Signature=(64 hex digits)
X-Amz-SignedHeaders=host
response-content-disposition=attachment; filename="Q3 r_sum_ __.csv"; filename*=UTF-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
response-content-type=text/csv
x-amz-checksum-mode=ENABLED
x-id=GetObject
the same, followed, first 100 bytes: curl -L -r 0-99
HTTP/1.1 303 See Other
location: http://127.0.0.1:35481/exports-bucket/acct_1/exp_42.csv?... X-Amz-Date=20261130T120000Z
HTTP/1.1 206 Partial Content
accept-ranges: bytes
etag: "9f11b5ee0ee007e4eaee6a0f221b69bf"
last-modified: Mon, 30 Nov 2026 11:00:00 GMT
content-disposition: attachment; filename="Q3 r_sum_ __.csv"; filename*=UTF-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
content-range: bytes 0-99/100010
content-length: 100
store: GET acct_1/exp_42.csv bytes=0-99, signed
The store answers the range itself, with the ETag and Accept-Ranges a download manager needs to
resume. The SDK added x-amz-checksum-mode and x-id to the query on its own, and both are
signed.
Encode the filename for Content-Disposition
A filename goes into the header twice, as RFC 6266
recommends: an ASCII filename for clients that ignore filename*, and filename* in the
RFC 8187 encoding, which names UTF-8
and percent-encodes it.
export function contentDisposition(name, type = 'attachment') {
const fallback = name.replace(/[^\x20-\x7e]|["\\%]/gu, '_')
const encoded = encodeURIComponent(name).replace(
/['()*]/g,
(c) => '%' + c.charCodeAt(0).toString(16).toUpperCase(),
)
return `${type}; filename="${fallback}"; filename*=UTF-8''${encoded}`
}
naive-header.mjs sends three names both ways through a Node server and reads the raw bytes back.
node naive-header.mjs
report.csv
filename="..." as written sent, ASCII only
contentDisposition() sent, ASCII only
Q3 résumé.csv
filename="..." as written sent, with the bytes e9 e9
contentDisposition() sent, ASCII only
Q3 報告.csv
filename="..." as written refused, ERR_INVALID_CHAR
contentDisposition() sent, ASCII only
Node sends é as the single Latin-1 byte e9, where UTF-8 would be c3 a9, and refuses a
character outside Latin-1 with ERR_INVALID_CHAR.
Hand the file to nginx instead
In accel mode the API answers 200 with no body and an X-Accel-Redirect header naming a path.
Mark the nginx location that holds the files
internal, so a client that
requests it directly gets 404. The same page lists requests redirected by X-Accel-Redirect from
an upstream server among the internal requests such a location serves.
node verify.mjs
X-Accel-Redirect for nginx: curl -I
HTTP/1.1 200 OK
x-accel-redirect: /internal-exports/acct_1/exp_42.csv
content-disposition: attachment; filename="Q3 r_sum_ __.csv"; filename*=UTF-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
On Apache, the third-party mod_xsendfile reads an
X-SENDFILE header instead and sends the named file in place of the handler’s output. Neither
server ran here. Either way the web server reads the file and sends it, so it carries the bytes
in place of the app.
Proxy through the app when you need control
http.ServeContent answers Range, If-Range and the conditional headers, but it never computes
an ETag. Its documentation says it uses an ETag the caller has set, so serve.go hashes the file
once and sets it.
go run ./cmd/servecontent
http.ServeContent: curl -I
HTTP/1.1 200 OK
accept-ranges: bytes
content-disposition: attachment; filename*=utf-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
content-length: 100010
etag: "fe6d808592ae5f0b091a21b52a392b22"
last-modified: Mon, 30 Nov 2026 11:00:00 GMT
first 100 bytes: curl -r 0-99
HTTP/1.1 206 Partial Content
accept-ranges: bytes
content-disposition: attachment; filename*=utf-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
content-length: 100
content-range: bytes 0-99/100010
etag: "fe6d808592ae5f0b091a21b52a392b22"
last-modified: Mon, 30 Nov 2026 11:00:00 GMT
The control at the end of the same run sets no ETag, so the response carries none. Go’s
mime.FormatMediaType writes filename* alone for a non-ASCII name, with no ASCII fallback.
Express res.download builds the header
with the content-disposition package, which puts a ? for each non-ASCII character in the
fallback, and passes the file to res.sendFile, which sets a weak ETag.1
node verify.mjs
proxied by Express, first 100 bytes: curl -r 0-99
HTTP/1.1 206 Partial Content
content-disposition: attachment; filename="Q3 r?sum? ??.csv"; filename*=UTF-8''Q3%20r%C3%A9sum%C3%A9%20%E5%A0%B1%E5%91%8A.csv
accept-ranges: bytes
last-modified: Mon, 30 Nov 2026 11:00:00 GMT
etag: W/"186aa-1a22c1c9380"
content-range: bytes 0-99/100010
content-length: 100
Check it worked
The Node tests follow each redirect against the stand-in and move the clock past expiry. They
replay the URL with another method, a changed or repeated parameter, and a scope for another
Region. They also check that the API sends no file bytes, makes no request to the
bucket, and signs the same URL whatever AWS_* variables are set.
node --test download.test.mjs
1..13
# tests 13
# suites 0
# pass 13
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 1606.121303
The Go tests cover the 206, If-Range with a matching and a stale ETag, the handler that sets
none, and the authorization.
node check-go.mjs
pass TestFilenameGoesOutAsFilenameStarOnly
pass TestIfRangeResumesOnlyWhileTheETagMatches
pass TestRangeIsA206WithTheETag
pass TestServeContentSendsNoETagUnlessTheHandlerSetsOne
pass TestTheHandlerAuthorizesFirst
5 tests, 0 not passed
When it goes wrong
A slow download fails partway with 403. The presigned URL expired, and a resume reused it. Each ranged request is a request of its own, checked against the expiry, so send the client back to the API for a fresh URL.
node verify.mjs
301 seconds later, resuming on the first URL: curl -r 50000-
HTTP/1.1 403 Forbidden
store: GET acct_1/exp_42.csv bytes=50000-, AccessDenied
resuming through the API instead: curl -L -r 50000-
HTTP/1.1 303 See Other
location: http://127.0.0.1:35481/exports-bucket/acct_1/exp_42.csv?... X-Amz-Date=20261130T120501Z
HTTP/1.1 206 Partial Content
curl -I -L reports 403 for a URL that a GET request fetches.
node verify.mjs
HEAD followed to the store: curl -I -L
HTTP/1.1 303 See Other
location: http://127.0.0.1:46537/exports-bucket/acct_1/exp_42.csv?... X-Amz-Date=20261130T120000Z
HTTP/1.1 403 Forbidden
store: HEAD acct_1/exp_42.csv whole object, SignatureDoesNotMatch
A HEAD request reached a URL signed for GET, and Signature Version 4 puts the method first in the
canonical request.
Check with a ranged GET, curl -L -r 0-99, as verify.mjs does.
Node throws ERR_INVALID_CHAR for one export and not another. The header carried the filename as
written, and Node accepts Latin-1 but refuses any character beyond it. Send both parameters, as
contentDisposition does.
When not to do this
Do not presign when you must stop a download partway. A presigned URL works for anyone who holds it until it expires, so a revoked customer keeps the file they started, and S3 lets one live up to seven days. Proxy through the app, or keep the expiry short.
Do not set the expiry from the file size alone. A URL signed with temporary credentials stops
working when those credentials expire, whatever X-Amz-Expires says.
Do not let a cache keep the 303. A stored redirect hands the next caller a URL that has expired, or
one signed for somebody else, which is why app.mjs sends Cache-Control: no-store with it.
Do not add nginx for this alone. It keeps the URL private where nginx already fronts the app, but an added proxy tier is a second server to configure, deploy, and keep in step with the API.
Related how-tos
Last verified
Verified 2026-11-30 against Node 22.23.5, Go 1.24.7 and Go 1.25.14, curl 8.5.0,
@aws-sdk/client-s3 and @aws-sdk/s3-request-presigner 3.1180.0, and express 5.3.0 with
send 1.2.1 and content-disposition 2.0.1. Every output block is what the command preceding it
printed. The presigned URLs were checked by a local stand-in, not by S3, Google Cloud Storage, or
Azure. nginx and Apache were not run, and their behavior is read from their documentation.
Footnotes
-
RFC 9110 forbids a client to put a weak entity tag in
If-Range. It also forbids a date there while the client holds any entity tag, so a client that follows it resumes an Express download with noIf-Rangeat all. Thesendmodule underres.sendFilechecksIf-RangewithindexOfat 1.2.1, and the tests record a 206 for the weak tag it issued, sent anyway. ↩︎ Back to text