UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

52 lines (49 loc) 1.74 kB
import { formatBite } from '../../bite/format-bite.mjs'; import { getCosNodejsSdkV5 } from '../../third-party/cos-nodejs-sdk-v5.mjs'; /** * 获取腾讯云 COS SDK 实例 * 创建并返回一个配置好的 COS 客户端实例 * @param secretId - 腾讯云 SecretId * @param secretKey - 腾讯云 SecretKey * @returns COS SDK 实例 * @example * ```ts * const cos = getCOSInstance('your-secret-id', 'your-secret-key'); * // 使用 cos 实例进行文件操作 * ``` */ function getCOSInstance(secretId, secretKey) { var COS = getCosNodejsSdkV5(); var cos = new COS({ SecretId: secretId, SecretKey: secretKey }); return cos; } /** * COS 文件上传进度回调函数 * 显示上传进度、速度、已上传大小和总大小 * @param info - 上传进度信息 * @param info.percent - 上传进度百分比(0-1) * @param info.speed - 上传速度(字节/秒) * @param info.total - 文件总大小(字节) * @param info.loaded - 已上传大小(字节) * @example * ```ts * onUploadCOSProgress({ * percent: 0.5, * speed: 1024000, * total: 10240000, * loaded: 5120000 * }); * // 输出: 总共:9.77 MB,已上传:4.88 MB,进度:50%,速度:1000.00 KB/s * ``` */ function onUploadCOSProgress(info) { var percent = parseInt("".concat(info.percent * 10000), 10) / 100; var speed = formatBite(info.speed || 0); var total = formatBite(info.total || 0); var loaded = formatBite(info.loaded || 0); console.log("[uploadCOSFile] \u603B\u5171\uFF1A".concat(total, "\uFF0C\u5DF2\u4E0A\u4F20\uFF1A").concat(loaded, "\uFF0C\u8FDB\u5EA6\uFF1A").concat(percent, "%\uFF0C\u901F\u5EA6\uFF1A").concat(speed, "/s;")); } export { getCOSInstance, onUploadCOSProgress };