|
|
-
-
- import OSS from "ali-oss"
- import { v4 as uuidv4 } from 'uuid';
-
- function uploadFileToOSS(file) {
-
- uni.showLoading({
- title: '图片上传中...'
- });
-
- return new Promise((resolve,reject) => {
- // 创建OSS实例
- const client = new OSS({
- // region: 'oss-cn-shenzhen',
- // accessKeyId: 'LTAI5tMan18fjJPUtr3Aim2W',
- // accessKeySecret: 'lhALqqgYijc115wY8c1KfTYkbSnq5I',
- // bucket: 'mangoimageapplet',
- // endpoint:'oss-cn-shenzhen.aliyuncs.com'
-
- //桶的地址
- region: 'oss-cn-guangzhou',
- //id
- accessKeyId:'LTAI5tNycA46YTwm383dRvMV',
- //密钥
- accessKeySecret:'tAdbYQCmdur6jbZ8hjvgB7T1Z52mIG',
- //桶的名字
- bucket: 'zhuoqiu-image',
- endpoint:'oss-cn-guangzhou.aliyuncs.com',
- });
-
- // 设置文件名和文件目录
- const suffix = '.' + file.name.split('.').pop();
- const fileName = uuidv4() + suffix; // 注意:文件名需要是唯一的
-
- // 使用put接口上传文件
- client.multipartUpload(fileName, file, {
- headers: {
- 'Content-Disposition': 'inline',
- 'Content-Type': file.type
- }
- }).then(res => {
- uni.hideLoading();
- resolve('https://tennis-oss.xzaiyp.top/' + res.name);
- }).catch(err => {
- uni.hideLoading();
- reject(err)
- })
- })
- }
-
-
- export default uploadFileToOSS
|