1.0.1 • Published 1 year ago

xiatianwhitedeyzeta v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

安装

npm run @rays/zeta-uploader

获取授权

获取腾讯云cos/ceph对象存储临时授权 授权时长24小时(超时需要重新调用此接口获取授权信息) const authorizeInfo = { tmpSecretId: string; tmpSecretKey: string; sessionToken: string; bucket: string; region: string; expiredTime: number; storageType: string; endPoint?: string; } const type = 'ceph' type 不传默认腾讯云cos const zetaUploader = new ZetaUploader(authorizeInfo,type?)

分片上传

cos:默认一片1010241024

ceph: 默认610241024 ceph 规定最后一片小于510241024

export async function sliceUploadFileAxios( file: File, cb: (taskId: string | { uploadId: string, key: string }, progressData: any, data: any) => void ) { const token = storageCache.get("auth"); const userId = storageCache.get("userId"); // key: 对于断点续传的文件 const md5 = await calcFileMD5(file); const index = file.name.lastIndexOf(".") const key = ${token.tenantId}/${userId}/${md5}.${file.name.substring(index + 1)}; fileUploadClient.sliceUploadFileAxios(key, file, cb) }

cos取消上传

export async function cancelTaskAxios(taskId: string) { fileUploadClient.cancelTaskAxios(taskId) }

ceph取消上传

export async function cancelUploadIdAxios(uploadId: string, key: string) { fileUploadClient.cancelUploadIdAxios(uploadId, key) }

图片上传 超过一M会压缩

export async function uploadImage( file: File, cb: (data: any, key: string) => void ): Promise { const token = storageCache.get("auth"); const userId = storageCache.get("userId"); const index = file.name.lastIndexOf(".") const key = ${token.tenantId}/${userId}/${uuid.v1()}.${file.name.substring(index + 1)}; fileUploadClient.uploadImage(key, file, cb) }

下载文件-Blob格式

export async function downloadFileAxios( key: string, cb?: (progressData: any) => void ) { if (getStorageType() === 'ceph') { fileUploadClient.downloadFileAxios(key.charAt(0) === '/' ? key.replace(/\//, '') : key, cb) } else { fileUploadClient.downloadFileAxios(key, cb) } }

下载文件-Url格式

export async function downloadFileUrlAxios(key: string, filename?: string) { if (getStorageType() === 'ceph') { fileUploadClient.downloadFileUrlAxios(key, filename!) } else { return fileUploadClient.downloadFileUrlAxios(key) } }