小壹安装包下载签名改在版本模块内完成,不改动素材预览依赖的 s3 模块
This commit is contained in:
parent
a897860377
commit
ef2023e7cf
@ -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
|
||||
|
||||
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
12
lib/s3.ts
12
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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user