feat: 初始化分片等方法

This commit is contained in:
Pan Qiancheng 2026-01-14 15:53:30 +08:00
parent 25ac418fc1
commit 9516284bc9
10 changed files with 342 additions and 0 deletions

View File

@ -30,6 +30,27 @@ export declare class ALiYunInstance {
bucket: string; bucket: string;
name: string; name: string;
}>; }>;
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
presignMulti(bucket: string, zone: ALiYunZone, key: string, uploadId: string, from: number, to: number, options?: {
expires?: number;
headers?: Record<string, string>;
timeout?: number;
stsToken?: string;
accessKeyId?: string;
accessKeySecret?: string;
}): Promise<{
partNumber: number;
uploadUrl: string;
}[]>;
/** /**
* URL * URL
* uploadId URL * uploadId URL

View File

@ -207,6 +207,50 @@ export class ALiYunInstance {
}); });
} }
} }
/**
* 为分片上传生成预签名 URL
* @param bucket
* @param key 对象键
* @param uploadId 上传 ID
* @param from 起始分片号
* @param to 结束分片号
* @param options 配置项
* @returns 分片预签名 URL 列表
*/
async presignMulti(bucket, zone, key, uploadId, from, to, options) {
const client = new OSS({
region: `oss-${zone}`,
accessKeyId: options?.accessKeyId || this.accessKey,
accessKeySecret: options?.accessKeySecret || this.secretKey,
bucket: bucket,
stsToken: options?.stsToken,
});
try {
const presignedUrls = [];
const expires = options?.expires || 3600;
for (let i = from; i <= to; i++) {
const uploadUrl = client.signatureUrl(key, {
method: 'PUT',
expires: expires,
subResource: {
uploadId: uploadId,
partNumber: String(i),
},
"Content-Type": "application/octet-stream",
});
presignedUrls.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return presignedUrls;
}
catch (error) {
throw new OakExternalException('aliyun', error.code, error.message, {
status: error.status,
});
}
}
/** /**
* 准备分片上传初始化并生成所有分片的预签名URL * 准备分片上传初始化并生成所有分片的预签名URL
* 用于前端直传场景返回 uploadId 和每个分片的上传 URL * 用于前端直传场景返回 uploadId 和每个分片的上传 URL

17
es/service/s3/S3.d.ts vendored
View File

@ -36,6 +36,23 @@ export declare class S3Instance {
bucket: string; bucket: string;
key: string; key: string;
}>; }>;
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
presignMulti(bucket: string, key: string, uploadId: string, from: number, to: number, options?: {
endpoint?: string;
expiresIn?: number;
}): Promise<{
partNumber: number;
uploadUrl: string;
}[]>;
/** /**
* URL * URL
* uploadId URL * uploadId URL

View File

@ -143,6 +143,40 @@ export class S3Instance {
throw new OakExternalException('s3', err.code, err.message, err, 'oak-external-sdk', {}); throw new OakExternalException('s3', err.code, err.message, err, 'oak-external-sdk', {});
} }
} }
/**
* 为分片上传生成预签名 URL
* @param bucket
* @param key 对象键
* @param uploadId 上传 ID
* @param from 起始分片号
* @param to 结束分片号
* @param options 配置项
* @returns 分片预签名 URL 列表
*/
async presignMulti(bucket, key, uploadId, from, to, options) {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)
: this.client;
// 2. 为每个分片生成预签名 URL
const parts = [];
const expiresIn = options?.expiresIn || 3600;
for (let i = from; i <= to; i++) {
const uploadCommand = new UploadPartCommand({
Bucket: bucket,
Key: key,
UploadId: uploadId,
PartNumber: i,
});
const uploadUrl = await getSignedUrl(client, uploadCommand, {
expiresIn: expiresIn,
});
parts.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return parts;
}
/** /**
* 准备分片上传创建分片上传并生成所有分片的预签名URL * 准备分片上传创建分片上传并生成所有分片的预签名URL
* 用于前端直传场景返回 uploadId 和每个分片的上传 URL * 用于前端直传场景返回 uploadId 和每个分片的上传 URL

View File

@ -30,6 +30,27 @@ export declare class ALiYunInstance {
bucket: string; bucket: string;
name: string; name: string;
}>; }>;
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
presignMulti(bucket: string, zone: ALiYunZone, key: string, uploadId: string, from: number, to: number, options?: {
expires?: number;
headers?: Record<string, string>;
timeout?: number;
stsToken?: string;
accessKeyId?: string;
accessKeySecret?: string;
}): Promise<{
partNumber: number;
uploadUrl: string;
}[]>;
/** /**
* URL * URL
* uploadId URL * uploadId URL

View File

@ -211,6 +211,50 @@ class ALiYunInstance {
}); });
} }
} }
/**
* 为分片上传生成预签名 URL
* @param bucket
* @param key 对象键
* @param uploadId 上传 ID
* @param from 起始分片号
* @param to 结束分片号
* @param options 配置项
* @returns 分片预签名 URL 列表
*/
async presignMulti(bucket, zone, key, uploadId, from, to, options) {
const client = new ali_oss_1.default({
region: `oss-${zone}`,
accessKeyId: options?.accessKeyId || this.accessKey,
accessKeySecret: options?.accessKeySecret || this.secretKey,
bucket: bucket,
stsToken: options?.stsToken,
});
try {
const presignedUrls = [];
const expires = options?.expires || 3600;
for (let i = from; i <= to; i++) {
const uploadUrl = client.signatureUrl(key, {
method: 'PUT',
expires: expires,
subResource: {
uploadId: uploadId,
partNumber: String(i),
},
"Content-Type": "application/octet-stream",
});
presignedUrls.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return presignedUrls;
}
catch (error) {
throw new Exception_1.OakExternalException('aliyun', error.code, error.message, {
status: error.status,
});
}
}
/** /**
* 准备分片上传初始化并生成所有分片的预签名URL * 准备分片上传初始化并生成所有分片的预签名URL
* 用于前端直传场景返回 uploadId 和每个分片的上传 URL * 用于前端直传场景返回 uploadId 和每个分片的上传 URL

View File

@ -36,6 +36,23 @@ export declare class S3Instance {
bucket: string; bucket: string;
key: string; key: string;
}>; }>;
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
presignMulti(bucket: string, key: string, uploadId: string, from: number, to: number, options?: {
endpoint?: string;
expiresIn?: number;
}): Promise<{
partNumber: number;
uploadUrl: string;
}[]>;
/** /**
* URL * URL
* uploadId URL * uploadId URL

View File

@ -146,6 +146,40 @@ class S3Instance {
throw new types_1.OakExternalException('s3', err.code, err.message, err, 'oak-external-sdk', {}); throw new types_1.OakExternalException('s3', err.code, err.message, err, 'oak-external-sdk', {});
} }
} }
/**
* 为分片上传生成预签名 URL
* @param bucket
* @param key 对象键
* @param uploadId 上传 ID
* @param from 起始分片号
* @param to 结束分片号
* @param options 配置项
* @returns 分片预签名 URL 列表
*/
async presignMulti(bucket, key, uploadId, from, to, options) {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)
: this.client;
// 2. 为每个分片生成预签名 URL
const parts = [];
const expiresIn = options?.expiresIn || 3600;
for (let i = from; i <= to; i++) {
const uploadCommand = new client_s3_1.UploadPartCommand({
Bucket: bucket,
Key: key,
UploadId: uploadId,
PartNumber: i,
});
const uploadUrl = await (0, s3_request_presigner_1.getSignedUrl)(client, uploadCommand, {
expiresIn: expiresIn,
});
parts.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return parts;
}
/** /**
* 准备分片上传创建分片上传并生成所有分片的预签名URL * 准备分片上传创建分片上传并生成所有分片的预签名URL
* 用于前端直传场景返回 uploadId 和每个分片的上传 URL * 用于前端直传场景返回 uploadId 和每个分片的上传 URL

View File

@ -234,6 +234,66 @@ export class ALiYunInstance {
} }
} }
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
async presignMulti(
bucket: string,
zone: ALiYunZone,
key: string,
uploadId: string,
from: number,
to: number,
options?: {
expires?: number; // URL 过期时间(秒),默认 3600
headers?: Record<string, string>;
timeout?: number;
stsToken?: string;
accessKeyId?: string;
accessKeySecret?: string;
}
) {
const client = new OSS({
region: `oss-${zone}`,
accessKeyId: options?.accessKeyId || this.accessKey,
accessKeySecret: options?.accessKeySecret || this.secretKey,
bucket: bucket,
stsToken: options?.stsToken,
});
try {
const presignedUrls: Array<{ partNumber: number; uploadUrl: string }> =
[];
const expires = options?.expires || 3600;
for (let i = from; i <= to; i++) {
const uploadUrl = client.signatureUrl(key, {
method: 'PUT',
expires: expires,
subResource: {
uploadId: uploadId,
partNumber: String(i),
},
"Content-Type": "application/octet-stream",
} as any);
presignedUrls.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return presignedUrls;
} catch (error: any) {
throw new OakExternalException('aliyun', error.code, error.message, {
status: error.status,
});
}
}
/** /**
* URL * URL
* uploadId URL * uploadId URL

View File

@ -231,6 +231,56 @@ export class S3Instance {
} }
} }
/**
* URL
* @param bucket
* @param key
* @param uploadId ID
* @param from
* @param to
* @param options
* @returns URL
*/
async presignMulti(
bucket: string,
key: string,
uploadId: string,
from: number,
to: number,
options?: {
endpoint?: string;
expiresIn?: number; // URL 过期时间(秒),默认 3600
}
) {
const client = options?.endpoint
? this.createClient(options.endpoint, this.defaultRegion)
: this.client;
// 2. 为每个分片生成预签名 URL
const parts: Array<{ partNumber: number; uploadUrl: string }> = [];
const expiresIn = options?.expiresIn || 3600;
for (let i = from; i <= to; i++) {
const uploadCommand = new UploadPartCommand({
Bucket: bucket,
Key: key,
UploadId: uploadId,
PartNumber: i,
});
const uploadUrl = await getSignedUrl(client, uploadCommand, {
expiresIn: expiresIn,
});
parts.push({
partNumber: i,
uploadUrl: uploadUrl,
});
}
return parts;
}
/** /**
* URL * URL
* uploadId URL * uploadId URL