fix: S3 可选zone

This commit is contained in:
Pan Qiancheng 2025-12-29 16:36:34 +08:00
parent 7a25050213
commit 25ac418fc1
5 changed files with 141 additions and 14 deletions

View File

@ -94,7 +94,7 @@ export declare class S3Instance {
/**
* URL
*/
presignObjectUrl(method: 'GET' | 'PUT' | 'POST' | 'DELETE', bucket: string, zone: S3Zone, key: string, options?: {
presignObjectUrl(method: 'GET' | 'PUT' | 'POST' | 'DELETE', bucket: string, zone: S3Zone | undefined, key: string, options?: {
expires?: number;
endpoint?: string;
}): Promise<{

View File

@ -313,7 +313,7 @@ export class S3Instance {
/**
* 获取预签名对象URL统一接口
*/
async presignObjectUrl(method, bucket, zone, key, options) {
async presignObjectUrl(method, bucket, zone = '', key, options) {
try {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)

View File

@ -94,7 +94,7 @@ export declare class S3Instance {
/**
* URL
*/
presignObjectUrl(method: 'GET' | 'PUT' | 'POST' | 'DELETE', bucket: string, zone: S3Zone, key: string, options?: {
presignObjectUrl(method: 'GET' | 'PUT' | 'POST' | 'DELETE', bucket: string, zone: S3Zone | undefined, key: string, options?: {
expires?: number;
endpoint?: string;
}): Promise<{

View File

@ -316,7 +316,7 @@ class S3Instance {
/**
* 获取预签名对象URL统一接口
*/
async presignObjectUrl(method, bucket, zone, key, options) {
async presignObjectUrl(method, bucket, zone = '', key, options) {
try {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)

View File

@ -12,6 +12,7 @@ import {
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { S3Zone } from '../../types';
import { OakExternalException } from 'oak-domain/lib/types';
export class S3Instance {
private accessKey: string;
@ -92,7 +93,14 @@ export class S3Instance {
accessKey: this.accessKey,
};
} catch (err: any) {
throw new Error(`生成S3上传URL失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -117,7 +125,14 @@ export class S3Instance {
await client.send(command);
} catch (err: any) {
throw new Error(`删除S3文件失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -146,7 +161,14 @@ export class S3Instance {
if (err.name === 'NotFound' || err.$metadata?.httpStatusCode === 404) {
return false;
}
throw err;
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -198,7 +220,14 @@ export class S3Instance {
key: response.Key!,
};
} catch (err: any) {
throw new Error(`创建分片上传失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -258,7 +287,14 @@ export class S3Instance {
parts: parts,
};
} catch (err: any) {
throw new Error(`准备分片上传失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -292,7 +328,14 @@ export class S3Instance {
eTag: response.ETag!,
};
} catch (err: any) {
throw new Error(`上传分片失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -331,7 +374,14 @@ export class S3Instance {
eTag: response.ETag,
};
} catch (err: any) {
throw new Error(`完成分片上传失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -357,7 +407,14 @@ export class S3Instance {
await client.send(command);
} catch (err: any) {
throw new Error(`中止分片上传失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -395,7 +452,14 @@ export class S3Instance {
nextPartNumberMarker: response.NextPartNumberMarker,
};
} catch (err: any) {
throw new Error(`列出分片失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
@ -421,7 +485,70 @@ export class S3Instance {
const url = await getSignedUrl(client, command, { expiresIn });
return url;
} catch (err: any) {
throw new Error(`生成预签名URL失败: ${err.message}`);
throw new OakExternalException(
's3',
err.code,
err.message,
err,
'oak-external-sdk',
{}
);
}
}
/**
* URL
*/
async presignObjectUrl(
method: 'GET' | 'PUT' | 'POST' | 'DELETE',
bucket: string,
zone: S3Zone = '',
key: string,
options?: {
expires?: number;
endpoint?: string;
}
): Promise<{
url: string;
headers?: Record<string, string | string[]>;
formdata?: Record<string, any>;
}> {
try {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)
: this.client;
let req;
switch (method) {
case 'GET':
req = new GetObjectCommand({ Bucket: bucket, Key: key });
break;
case 'PUT':
req = new PutObjectCommand({ Bucket: bucket, Key: key });
break;
case 'DELETE':
req = new DeleteObjectCommand({ Bucket: bucket, Key: key });
break;
case 'POST':
throw new Error('S3 不支持 POST 方法的预签名 URL');
}
const url = await getSignedUrl(client, req, {
expiresIn: options?.expires || 3600,
});
return { url };
} catch (error: any) {
throw new OakExternalException(
's3',
error.code,
error.message,
error,
'oak-external-sdk',
{}
);
}
}
}