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.

67 lines
1.6 KiB

8 months ago
8 months ago
8 months ago
8 months ago
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. */
  27. //桶的地址
  28. region: 'oss-cn-guangzhou',
  29. //id
  30. accessKeyId: 'LTAI5tQSs47izVy8DLVdwUU9',
  31. //密钥
  32. accessKeySecret: 'qHI7C3PaXYZySr84HTToviC71AYlFq',
  33. //桶的名字
  34. bucket: 'hanhaiimage',
  35. endpoint: 'oss-cn-shenzhen.aliyuncs.com',
  36. });
  37. // 设置文件名和文件目录
  38. const suffix = '.' + file.name.split('.').pop();
  39. const fileName = uuidv4() + suffix; // 注意:文件名需要是唯一的
  40. // 使用put接口上传文件
  41. client.multipartUpload(fileName, file, {
  42. headers: {
  43. 'Content-Disposition': 'inline',
  44. 'Content-Type': file.type
  45. }
  46. }).then(res => {
  47. uni.hideLoading();
  48. // resolve('https://tennis-oss.xzaiyp.top/' + res.name);
  49. resolve('https://image.hhlm1688.com/' + res.name);
  50. }).catch(err => {
  51. uni.hideLoading();
  52. reject(err)
  53. })
  54. })
  55. }
  56. export default uploadFileToOSS