diff --git a/lib/__tests__/desktop-client-release.test.ts b/lib/__tests__/desktop-client-release.test.ts index 5f63adb2..4c79d4cf 100644 --- a/lib/__tests__/desktop-client-release.test.ts +++ b/lib/__tests__/desktop-client-release.test.ts @@ -13,7 +13,7 @@ import { s3Client } from '@/lib/s3' import { publishXiaoyiClient } from '@/scripts/publish-xiaoyi-client' jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() })) -jest.mock('@/lib/s3', () => ({ s3Client: { send: jest.fn() }, getPublicS3Client: jest.fn() })) +jest.mock('@/lib/s3', () => ({ s3Client: { send: jest.fn() } })) jest.mock('@aws-sdk/s3-request-presigner', () => ({ getSignedUrl: jest.fn() })) const send = s3Client.send as jest.Mock diff --git a/lib/desktop-client/client-release.ts b/lib/desktop-client/client-release.ts index 4cdb5fcd..7ca9ce02 100644 --- a/lib/desktop-client/client-release.ts +++ b/lib/desktop-client/client-release.ts @@ -1,6 +1,6 @@ -import { GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3' +import { GetObjectCommand, HeadObjectCommand, S3Client } from '@aws-sdk/client-s3' import { getSignedUrl } from '@aws-sdk/s3-request-presigner' -import { getPublicS3Client, s3Client } from '../s3' +import { s3Client } from '../s3' export const semver = require('semver') as { valid(version: unknown): string | null @@ -73,5 +73,17 @@ export async function signReleaseDownload(channel: string, release: ClientReleas Key: `${releasePrefix(channel)}/${release.version}/${release.file.name}`, ResponseContentDisposition: `attachment; filename*=UTF-8''${encodeURIComponent(release.file.name).replace(/['()*]/g, char => '%' + char.charCodeAt(0).toString(16))}`, }) - return getSignedUrl(getPublicS3Client(), command, { expiresIn: 600 }) + return getSignedUrl(publicS3Client(), command, { expiresIn: 600 }) +} + +// 下载地址按公网存储域名签名,客户端在内网和外网都能直接下载。 +function publicS3Client(): S3Client { + const domain = process.env.S3_PUBLIC_DOMAIN?.trim() + if (!domain) return s3Client + return new S3Client({ + endpoint: new URL(domain).origin, + region: process.env.S3_REGION || 'us-east-1', + credentials: { accessKeyId: process.env.S3_ACCESS_KEY_ID!, secretAccessKey: process.env.S3_SECRET_ACCESS_KEY! }, + forcePathStyle: true, + }) } diff --git a/lib/s3.ts b/lib/s3.ts index 031bfba3..002b4eb3 100644 --- a/lib/s3.ts +++ b/lib/s3.ts @@ -1,5 +1,4 @@ import { S3Client, PutObjectCommand, DeleteObjectCommand, GetObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3' -import type { S3ClientConfig } from '@aws-sdk/client-s3' import { createReadStream, createWriteStream } from 'fs' import { lstat } from 'fs/promises' import { Readable } from 'stream' @@ -162,7 +161,7 @@ function cleanS3Endpoint(endpoint: string | undefined): string { * - 保留 adaptive 重试模式(自动调整重试间隔) * - 添加更详细的日志记录 */ -const s3Config = { +const s3Client = new S3Client({ endpoint: cleanS3Endpoint(process.env.S3_ENDPOINT), region: process.env.S3_REGION || 'us-east-1', credentials: { @@ -180,14 +179,7 @@ const s3Config = { // 5次重试足以应对临时网络抖动,同时避免长时间阻塞 maxAttempts: 5, retryMode: 'adaptive', // 自动调整重试间隔(指数退避 + 抖动) -} satisfies S3ClientConfig - -const s3Client = new S3Client(s3Config) - -export function getPublicS3Client(): S3Client { - const domain = process.env.S3_PUBLIC_DOMAIN?.trim() - return domain ? new S3Client({ ...s3Config, endpoint: new URL(domain).origin }) : s3Client -} +}) export async function uploadToS3( file: Buffer,