@bnfe/oss-upload
v1.0.8
Published
oss-upload
Downloads
24
Keywords
Readme
@bnfe/oss-upload
这是一个集成ali-oss上传的库
安装
pnpm install @bnfe/oss-upload -S引入
import { AliOss } from "@bnfe/oss-upload";使用
const aliOss = new AliOss({
dir: "/sop", // 上传文件目录 需要更改为你要上传的目录
reqUrl: "", // 获取token的地址
});
export const OssUpload = async (file: File) => {
try {
const result: any = await aliOss.upload(file);
return result.cdnUrl;
} catch (error) {
console.log("error", error);
return new Error("上传失败");
}
};
入参
type MultipartUploadParamsTypes = {
parallel?: number; // 并行上传数量 默认为5
partSize?: number; // 每片大小 默认1M
checkpoint?: any; // 断点续传时需要的参数 为progress的checkpoint
/**
* 分片上传需要进度条时可以设置此参数
* p 进度百分比(0~1之间的小数)
* fileUrl 为 cdn返回的url
* checkpoint记录本地分片上传结果的文件
* res 单个分片上传成功返回的response。
*/
progress?: (p: number, fileUrl: string, checkpoint?: any, res?: any) => void;
};
type OssPropsTypes = {
/**
* 请求token的地址 默认为根据环境变量获取 传入则根据传入使用
* 传入时 该接口需返回
* {
* data: {
* host: cdn地址
* region: 阿里云配置
* bucket: 阿里云配置
* stsToken: 获取临时的token 临时token需要为STS
* accessKeyId: 获取临时的accessKeyId
* accessKeySecret: 获取临时的accessKeySecret
* expiration: 获取临时的expiration
* }
* }
*/
reqUrl: string;
// 文件夹名称 默认为 public 传入时不加 /
dir?: string;
// token过期时间 ms 默认为1小时 3600 60*60
expire?: number;
/** 超过多大开启分片上传 默认为 100MB 100 * 1024 * 1024 */
multipartUploadSize?: number;
multipartUploadParams?: MultipartUploadParamsTypes;
};
Upload方法入参
type uploadProps = {
file: File, // 文件
/**
* 文件名,传入直接使用name,不拼接dir
* 默认会使用dir+日期+uuid(6位)+文件名
*/
name?: string,
// 分片上传需要参数
multipartUploadParams?: MultipartUploadParamsTypes
}
出参
type ResParamsTypes = {
cdnUrl: string; // url 为cdn返回的url
ossUrl: string; // ossUrl为oss返回的url
name: string; // 文件名
ossResParams: any; // oss返回的参数
};
ali-oss
参考文档
