Repository navigation
Open
Description
Environment
- unstorage 1.17.6 (same code on
main,src/drivers/s3.ts:117-118) - aws4fetch 1.0.20
- Node.js 26.13.1
- Cloudflare R2 through its S3 API
Reproduction
An S3-compatible endpoint that answers the first request with 500, like a transient R2 error:
import { createServer } from 'node:http'
import { createStorage } from 'unstorage'
import s3Driver from 'unstorage/drivers/s3'
let requests = 0
const server = createServer((req, res) => {
requests++
req.resume()
req.on('end', () => {
res.statusCode = requests === 1 ? 500 : 200
res.end(requests === 1 ? 'We encountered an internal error. Please try again.' : '')
})
}).listen(0)
await new Promise(resolve => server.once('listening', resolve))
const storage = createStorage({
driver: s3Driver({
endpoint: `http://127.0.0.1:${server.address().port}`,
bucket: 'bucket',
region: 'auto',
accessKeyId: 'key',
secretAccessKey: 'secret',
}),
})
try {
await storage.setItemRaw('file.txt', 'hello')
console.log(`stored after ${requests} requests`)
}
catch (error) {
console.log(`failed after ${requests} request(s): ${error.message}`)
}
server.close()Output:
failed after 1 request(s): [unstorage] [s3] [PUT] http://127.0.0.1:54850/bucket/file.txt: 500 Internal Server Error We encountered an internal error. Please try again.
With the change below, the same script prints stored after 2 requests.
Describe the bug
The s3 driver signs each request with AwsClient.sign() and sends it with a plain fetch:
const request = await (await getAwsClient()).sign(url, opts);
const res = await fetch(request);That skips the retry loop in AwsClient.fetch(), which re-signs and resends on 5xx and 429 (10 retries by default, with exponential backoff and jitter). One transient error from the provider fails the whole operation.
We hit this in CI: a build stores about 680 files in R2 through the s3 driver, one PUT returned 500 InternalError ("We encountered an internal error. Please try again."), and the build failed.
Suggested fix, which keeps the 404 → null handling:
const awsFetch = async (url: string, opts?: RequestInit) => {
- const request = await (await getAwsClient()).sign(url, opts);
- const res = await fetch(request);
+ const res = await (await getAwsClient()).fetch(url, opts);
if (!res.ok) {
if (res.status === 404) {
return null;
}
throw createError(
DRIVER_NAME,
- `[${request.method}] ${url}: ${res.status} ${res.statusText} ${await res.text()}`,
+ `[${opts?.method ?? "GET"}] ${url}: ${res.status} ${res.statusText} ${await res.text()}`,
);
}
return res;
};Additional context
- The requests the driver sends (
GET,HEAD,PUT,DELETE, and thePOST ?deletebulk delete) are all safe to repeat. - We run this change as a local patch on 1.17.6 and can open a PR.
- Network errors (connection reset, DNS) still throw from
fetch, asAwsClient.fetch()only retries on response status.
Metadata
Metadata
Assignees
Labels
No labels
Activity