You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

8 months ago
  1. import OSS from "ali-oss"
  2. import { v4 as uuidv4 } from 'uuid';
  3. function uploadFileToOSS(file) {
  4. uni.showLoading({
  5. title: '图片上传中...'
  6. });
  7. return new Promise((resolve,reject) => {
  8. // 创建OSS实例
  9. const client = new OSS({
  10. // region: 'oss-cn-shenzhen',
  11. // accessKeyId: 'LTAI5tMan18fjJPUtr3Aim2W',
  12. // accessKeySecret: 'lhALqqgYijc115wY8c1KfTYkbSnq5I',
  13. // bucket: 'mangoimageapplet',
  14. // endpoint:'oss-cn-shenzhen.aliyuncs.com'
  15. //桶的地址
  16. region: 'oss-cn-guangzhou',
  17. //id
  18. accessKeyId:'LTAI5tNycA46YTwm383dRvMV',
  19. //密钥
  20. accessKeySecret:'tAdbYQCmdur6jbZ8hjvgB7T1Z52mIG',
  21. //桶的名字
  22. bucket: 'zhuoqiu-image',
  23. endpoint:'oss-cn-guangzhou.aliyuncs.com',
  24. });
  25. // 设置文件名和文件目录
  26. const suffix = '.' + file.name.split('.').pop();
  27. const fileName = uuidv4() + suffix; // 注意:文件名需要是唯一的
  28. // 使用put接口上传文件
  29. client.multipartUpload(fileName, file, {
  30. headers: {
  31. 'Content-Disposition': 'inline',
  32. 'Content-Type': file.type
  33. }
  34. }).then(res => {
  35. uni.hideLoading();
  36. resolve('https://tennis-oss.xzaiyp.top/' + res.name);
  37. }).catch(err => {
  38. uni.hideLoading();
  39. reject(err)
  40. })
  41. })
  42. }
  43. export default uploadFileToOSS