feat(upload): add 12-hour upload expiration and continuous backoff retry (#508)

* feat(upload): add 12-hour upload expiration and continuous backoff retry

* fix(upload): clamp jittered backoff delay to maxDelayMs and update presign JSDoc
This commit is contained in:
Yiling-J
2026-10-30 16:06:34 +08:00
committed by GitHub
parent 3b00a42a89
commit 9529c63c0a
7 changed files with 263 additions and 36 deletions
+73 -1
View File
@@ -40,6 +40,7 @@ describe('S3Service implementations', () => {
vi.clearAllMocks()
getSignedUrlSpy.mockReset()
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
getSignedUrlSpy.mockResolvedValue('http://presigned-url')
s3SendSpy.mockResolvedValue({})
})
@@ -146,7 +147,7 @@ describe('S3Service implementations', () => {
expect(getSignedUrlSpy).toHaveBeenCalledTimes(2)
})
it('should respect PRESIGNED_URL_EXPIRES_IN env', async () => {
it('should respect PRESIGNED_URL_EXPIRES_IN env for GET', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '10'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
@@ -159,6 +160,77 @@ describe('S3Service implementations', () => {
expect.objectContaining({ expiresIn: 10 * 3600 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
})
it('should default PUT presign and multipart to 12 hours (43200 seconds)', async () => {
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 12 * 3600 }),
)
getSignedUrlSpy.mockClear()
await s3.presignMultipart('bucket', 'key', {
method: 'PUT',
key: 'key',
fileId: 'file-1',
uploadId: 'up-1',
partNumber: 1,
})
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(UploadPartCommand),
expect.objectContaining({ expiresIn: 12 * 3600 }),
)
})
it('should respect PRESIGNED_UPLOAD_URL_EXPIRES_IN env for PUT and multipart', async () => {
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN = '24'
process.env.PRESIGNED_URL_EXPIRES_IN = '5'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 24 * 3600 }),
)
getSignedUrlSpy.mockClear()
await s3.presignMultipart('bucket', 'key', {
method: 'PUT',
key: 'key',
fileId: 'file-1',
})
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 24 * 3600 }),
)
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
it('should allow PRESIGNED_URL_EXPIRES_IN to override PUT if PRESIGNED_UPLOAD_URL_EXPIRES_IN is unset', async () => {
process.env.PRESIGNED_URL_EXPIRES_IN = '8'
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
getSignedUrlSpy.mockClear()
await s3.presign('bucket', 'key', 'PUT')
expect(getSignedUrlSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(PutObjectCommand),
expect.objectContaining({ expiresIn: 8 * 3600 }),
)
delete process.env.PRESIGNED_URL_EXPIRES_IN
})
+15 -4
View File
@@ -112,7 +112,7 @@ export interface S3Service {
download?: boolean,
filename?: string,
internal?: boolean,
/** URL lifetime; defaults to PRESIGNED_URL_EXPIRES_IN hours. Ignored by local storage. */
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET. Ignored by local storage. */
expiresInSeconds?: number,
) => Promise<string>
presignMultipart: (
@@ -364,8 +364,16 @@ export class S3StorageService implements S3Service {
internal?: boolean,
expiresInSeconds?: number,
): Promise<string> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const lifetimeSeconds = expiresInSeconds ?? expireHours * 3600
const defaultExpireHours =
method === 'PUT'
? parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN ||
process.env.PRESIGNED_URL_EXPIRES_IN ||
'12',
10,
)
: parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const lifetimeSeconds = expiresInSeconds ?? defaultExpireHours * 3600
// With an explicit (usually short) lifetime, only reuse a cached URL while it has at
// least half of its lifetime left, so callers never receive a nearly expired link.
const cacheTtlMs = expiresInSeconds ? (lifetimeSeconds * 1000) / 2 : lifetimeSeconds * 1000
@@ -416,7 +424,10 @@ export class S3StorageService implements S3Service {
key: string,
request: S3SignRequest,
): Promise<{ url: string }> {
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
const expireHours = parseInt(
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN || process.env.PRESIGNED_URL_EXPIRES_IN || '12',
10,
)
const expiresInSeconds = expireHours * 3600
let url: string