t-comm
Version:
专业、稳定、纯粹的工具库
173 lines (170 loc) • 5.79 kB
JavaScript
import { getFs } from '../../nodejs/fs.mjs';
import { getCOSInstance, onUploadCOSProgress } from './helper.mjs';
import '../../bite/format-bite.mjs';
import '../../third-party/cos-nodejs-sdk-v5.mjs';
// ========== ContentType 自动检测 ==========
var EXT_TO_CONTENT_TYPE = {
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.bmp': 'image/bmp',
'.ico': 'image/x-icon',
'.pdf': 'application/pdf',
'.json': 'application/json',
'.xml': 'application/xml',
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.txt': 'text/plain',
'.zip': 'application/zip',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.mp3': 'audio/mpeg',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.otf': 'font/otf'
};
function getContentTypeFromKey(key) {
var ext = key.substring(key.lastIndexOf('.')).toLowerCase();
return EXT_TO_CONTENT_TYPE[ext];
}
/**
* 以流式方式上传文件到腾讯云 COS(使用 putObject 简单上传)
* 支持通过文件路径或 Buffer 上传,适用于大文件或流式场景
* 注意:单次上传大小限制为 5GB,超大文件请使用 uploadCOSStreamFileV2
* @param {object} config 配置信息
* @param {object} config.file 文件信息
* @param {string} [config.file.path] 文件路径(与 buffer 二选一)
* @param {number} config.file.size 文件大小(字节)
* @param {Buffer} [config.file.buffer] 文件 Buffer(与 path 二选一)
* @param {string} config.key COS 对象键(如 'images/test.png')
* @param {string} config.secretId COS secretId
* @param {string} config.secretKey COS secretKey
* @param {string} config.bucket COS bucket
* @param {string} config.region COS region
* @returns {Promise<any>} 上传结果
* @example
* ```ts
* await uploadCOSStreamFile({
* file: { path: '/tmp/test.png', size: 1024 },
* key: 'images/test.png',
* secretId: 'xxx',
* secretKey: 'xxx',
* bucket: 'my-bucket-1250000000',
* region: 'ap-guangzhou',
* });
* ```
*/
function uploadCOSStreamFile(_ref) {
var file = _ref.file,
key = _ref.key,
secretId = _ref.secretId,
secretKey = _ref.secretKey,
bucket = _ref.bucket,
region = _ref.region;
return new Promise(function (resolve, reject) {
var cos = getCOSInstance(secretId, secretKey);
var body;
if (file.buffer) {
body = file.buffer;
} else if (file.path) {
body = getFs().createReadStream(file.path);
} else {
reject(new Error('file.path or file.buffer is required'));
return;
}
// https://cloud.tencent.com/document/product/436/64980
// putObject 简单上传文件
// uploadFile 高级上传,SDK 内部决定是进行分块上传还是简单上传
cos.putObject({
Bucket: bucket,
Region: region,
Key: key,
StorageClass: 'STANDARD',
Body: body,
ContentLength: file.size,
ContentType: getContentTypeFromKey(key),
Headers: {
'x-cos-traffic-limit': 819200 // 限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误。
},
onProgress: onUploadCOSProgress
}, function (err, data) {
if (data) {
resolve(data);
return;
}
reject(err);
});
});
}
/**
* 以高级上传方式上传文件到腾讯云 COS(使用 uploadFile,SDK 自动决定分块或简单上传)
* 适用于大文件上传,支持自动分块和断点续传
* @param {object} config 配置信息
* @param {object} config.file 文件信息
* @param {string} config.file.path 文件路径(必填)
* @param {number} config.file.size 文件大小(字节)
* @param {string} config.key COS 对象键(如 'images/test.png')
* @param {string} config.secretId COS secretId
* @param {string} config.secretKey COS secretKey
* @param {string} config.bucket COS bucket
* @param {string} config.region COS region
* @param {number} [config.sliceSize=5242880] 分块大小,默认 5MB
* @returns {Promise<any>} 上传结果
* @example
* ```ts
* await uploadCOSStreamFileV2({
* file: { path: '/tmp/large-file.zip', size: 104857600 },
* key: 'files/large-file.zip',
* secretId: 'xxx',
* secretKey: 'xxx',
* bucket: 'my-bucket-1250000000',
* region: 'ap-guangzhou',
* sliceSize: 1024 * 1024 * 10, // 10MB
* });
* ```
*/
function uploadCOSStreamFileV2(_ref2) {
var file = _ref2.file,
key = _ref2.key,
secretId = _ref2.secretId,
secretKey = _ref2.secretKey,
bucket = _ref2.bucket,
region = _ref2.region,
_ref2$sliceSize = _ref2.sliceSize,
sliceSize = _ref2$sliceSize === void 0 ? 1024 * 1024 * 5 : _ref2$sliceSize;
return new Promise(function (resolve, reject) {
var cos = getCOSInstance(secretId, secretKey);
if (!file.path) {
reject(new Error('file.path is required'));
return;
}
// https://cloud.tencent.com/document/product/436/64980
// putObject 简单上传文件
// uploadFile 高级上传,SDK 内部决定是进行分块上传还是简单上传
cos.uploadFile({
Bucket: bucket,
Region: region,
Key: key,
StorageClass: 'STANDARD',
FilePath: file.path,
ContentType: getContentTypeFromKey(key),
SliceSize: sliceSize,
Headers: {
'x-cos-traffic-limit': 819200 // 限速值设置范围为819200 - 838860800,即100KB/s - 100MB/s,如果超出该范围将返回400错误。
},
onProgress: onUploadCOSProgress
}, function (err, data) {
if (data) {
resolve(data);
return;
}
reject(err);
});
});
}
export { uploadCOSStreamFile, uploadCOSStreamFileV2 };