From 9caaf7162c2e79e2cd2677e8908de3d0801c8cc6 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Mon, 9 Nov 2026 10:31:30 -0700 Subject: [PATCH] fix(fs): follow S3 region redirects (#4544) A file row without a stored region is read with the server's configured region. When its bucket lives elsewhere, S3 answers 301 PermanentRedirect and the read failed with a 500. The SDK now retries once in the region the redirect names. --- src/backend/clients/s3/S3Client.test.ts | 73 +++++++++++++++++++++++++ src/backend/clients/s3/S3Client.ts | 3 + 2 files changed, 76 insertions(+) diff --git a/src/backend/clients/s3/S3Client.test.ts b/src/backend/clients/s3/S3Client.test.ts index 0ffdf21dc..5027d8741 100644 --- a/src/backend/clients/s3/S3Client.test.ts +++ b/src/backend/clients/s3/S3Client.test.ts @@ -21,6 +21,8 @@ import { GetObjectCommand } from '@aws-sdk/client-s3'; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { existsSync } from 'node:fs'; +import { createServer, type Server } from 'node:http'; +import type { AddressInfo } from 'node:net'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -200,6 +202,77 @@ describe('S3Client — remote endpoint configuration', () => { }); }); +describe('S3Client — bucket in another region', () => { + let server: Server; + let client: S3Client; + let signedRegions: string[]; + + // Answers like a bucket that lives in us-west-2: a request signed for any + // other region gets S3's 301 naming the bucket's region. + beforeEach(async () => { + signedRegions = []; + server = createServer((req, res) => { + const region = + /Credential=[^/]+\/\d{8}\/([^/]+)\//.exec( + req.headers.authorization ?? '', + )?.[1] ?? ''; + signedRegions.push(region); + if (region !== 'us-west-2') { + res.writeHead(301, { + 'content-type': 'application/xml', + 'x-amz-bucket-region': 'us-west-2', + }); + res.end( + 'PermanentRedirectThe bucket you are attempting to access must be addressed using the specified endpoint.', + ); + return; + } + res.writeHead(200, { + 'content-type': 'text/plain', + 'content-length': '5', + }); + res.end('hello'); + }); + await new Promise((resolve) => + server.listen(0, '127.0.0.1', resolve), + ); + const { port } = server.address() as AddressInfo; + client = new S3Client({ + port: 0, + extensions: [], + s3: { + s3Config: { + endpoint: `http://127.0.0.1:${port}`, + accessKeyId: 'fakeAccessKeyId', + secretAccessKey: 'fakeSecretAccessKey', + forcePathStyle: true, + }, + }, + } as unknown as IConfig); + await client.onServerStart(); + }); + + afterEach(async () => { + await client.onServerShutdown(); + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + }); + + it("retries a read in the bucket's region when S3 redirects it", async () => { + const result = await client + .get('ap-northeast-2') + .send( + new GetObjectCommand({ + Bucket: 'other-bucket', + Key: 'index.html', + }), + ); + + expect(await result.Body?.transformToString()).toBe('hello'); + expect(signedRegions).toEqual(['ap-northeast-2', 'us-west-2']); + }); +}); + describe('S3Client — legacy storage migration', () => { let client: S3Client; let dir: string; diff --git a/src/backend/clients/s3/S3Client.ts b/src/backend/clients/s3/S3Client.ts index 8dc49ef4d..a0e53bf23 100644 --- a/src/backend/clients/s3/S3Client.ts +++ b/src/backend/clients/s3/S3Client.ts @@ -235,6 +235,9 @@ export class S3Client extends PuterClient { const client = new AwsS3Client({ region, + // Rows without a stored region are read with the server's own, so + // retry where S3's 301 says the bucket actually lives. + followRegionRedirects: true, requestStreamBufferSize: 32 * 1024, requestHandler: new NodeHttpHandler({ socketTimeout: 5000,