parent
4597a8919e
commit
d0567119ab
9 changed files with 96 additions and 34 deletions
@ -0,0 +1,15 @@ |
||||
/** |
||||
* 阿里云oss配置 |
||||
* */ |
||||
|
||||
export default { |
||||
// oss账号信息
|
||||
config: { |
||||
region: 'oss-cn-shenzhen', |
||||
accessKeyId: 'LTAI4FzqQHnk4rozqLZ8jCNj', |
||||
accessKeySecret: 'mveW7B1OyFoKUkHm8WsxmrjHmkJWHq', |
||||
bucket: 'huoran' |
||||
}, |
||||
// 上传成功url前置部分(成功回调没有返回url)
|
||||
preUrl: 'https://huoran.oss-cn-shenzhen.aliyuncs.com/' |
||||
} |
@ -0,0 +1,37 @@ |
||||
// 阿里云oss上传
|
||||
|
||||
import OSS from 'ali-oss' |
||||
import OssConfig from './config' |
||||
import Util from '@/libs/util' |
||||
|
||||
let client = null |
||||
// 初始化oss
|
||||
const initOss = () => { |
||||
if (!client) client = new OSS(OssConfig.config) |
||||
} |
||||
|
||||
export default { |
||||
// 上传文件
|
||||
upload(file) { |
||||
initOss() |
||||
return new Promise(async (resolve, reject) => { |
||||
try { |
||||
// 上传到阿里云oss
|
||||
const res = await client.multipartUpload(file.name, file); |
||||
resolve({ |
||||
format: Util.getFileExt(file.name), |
||||
name: file.name, |
||||
url: OssConfig.preUrl + res.name, |
||||
size: file.size, |
||||
}) |
||||
} catch (error) { |
||||
reject() |
||||
} |
||||
}) |
||||
}, |
||||
// 删除文件(传完整url,不是没有https的name,因为很多接口没有存name,只存url,所以统一使用url)
|
||||
async del(url) { |
||||
initOss() |
||||
await client.delete(url.replace(OssConfig.preUrl, '')); |
||||
} |
||||
} |
Loading…
Reference in new issue