kk-oss
安装使用
安装包
npm install kk-oss ali-oss -S
cd 本项目
npm link
cd 项目
npm link kk-oss
copy 到项目中
"dependencies": {
"kk-oss": "file:./src/plugins/kk-oss"
},
使用包
import Vue from "vue";
import OSS, { ALI_OSS_API_URL } from "kk-oss";
import { post } from "@/项目中封装的公用post方法";
import { stsToken } from "@/项目中封装的请求oss配置接口方法";
Vue.use(OSS, {
fetchOSS: (data) => post(ALI_OSS_API_URL, data),
fetchOSS: stsToken,
});
上传
Options(Vue2 & 3)
await this.$oss.init()
import OSS from 'kk-oss'
const client = await OSS.instance.init()
console.log(this.$oss: OSS)
interface OSS {
client: AliOSS
config: {}
fetchOSS: () => Promise<{ data: any }>
generateFileName: (file: File, opts: IFilePathOptions) => string
getUploadFilePath: (file: File, opts: IFilePathOptions) => string
}
const filePath = this.$oss.getUploadFilePath(file, {
type: 'img',
business: 'kukenews/rich-text',
platform: 'web',
private: false
directory: '',
fileName: '',
})
const result = await this.$oss.client.put(filePath, file)
console.log(result)
Setup(Vue3)
vue3 组件封装参考 /example/src/components/KkUploadImage.vue
import useOSS from 'kk-oss/src/useOSS'
const { initOSS } = useOSS()
const $oss = await initOSS()
import OSS from 'kk-oss'
const client = await OSS.instance.init()
console.log($oss)
const filePath = $oss.getUploadFilePath(file, {
type: 'img',
business: 'kukenews/rich-text',
platform: 'web',
private: false
directory: '',
fileName: '',
})
const result = await $oss.client.put(filePath, file)
console.log(result)
下载
import {
downloadFileByURL,
downloadFileByData,
downloadFileByXHR,
} from "kk-oss";
downloadFileByURL(url, name, cb);
downloadFileByData(data, name, mimeType);
downloadFileByXHR(url, name, mimeType, headers);
可能出现的问题
- v0.0.11版本前会出现在 vue-cli 项目不能正常运行的问题
{
"transpileDependencies": ["kk-oss"],
}
- 如不能正常运行, 提示 @babel/runtime/**/* 错误
npm i babel-core 或 @babel/runtime