zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { BoleOss } from '@bole-core/core';
import { loadEnv } from '@build/index';
import AliOSS from 'ali-oss';
import * as userServices from '@/services/api/User';
 
export class OssManager {
  private static OssInstance: BoleOss;
 
  private static async getOssSTS() {
    return await userServices.getOssSTS({
      showLoading: false,
    });
  }
 
  public static async getOssInstance() {
    try {
      if (!this.OssInstance) {
        let res = await this.getOssSTS();
        const { VITE_OSS_URL_BUCKET } = loadEnv();
        this.OssInstance = new BoleOss({
          accessKeyId: res.ossAccessKeyId,
          accessKeySecret: res.ossAccessSecret,
          stsToken: res.securityToken,
          bucket: VITE_OSS_URL_BUCKET,
          endpoint: 'https://oss-cn-hangzhou.aliyuncs.com/',
          refreshSTSToken: async () => {
            const info = await this.getOssSTS();
            return {
              accessKeyId: info.ossAccessKeyId,
              accessKeySecret: info.ossAccessSecret,
              stsToken: info.securityToken,
            };
          },
          refreshSTSTokenInterval: 20 * 60 * 1000,
        });
      }
      return this.OssInstance;
    } catch (error) {
      console.log('error: ', error);
    }
  }
 
  public static async asyncUpload(uploadOptions: BoleOss.UploadOptions) {
    const instance = await this.getOssInstance();
    return instance.asyncUpload(uploadOptions);
  }
 
  public static async multipartUpload(
    uploadOptions: BoleOss.MultipartUploadOptions,
    options: AliOSS.MultipartUploadOptions
  ) {
    const { VITE_OSS_URL } = loadEnv();
    const instance = await this.getOssInstance();
    return instance.multipartUpload(
      {
        ossBaseUrl: VITE_OSS_URL,
        ...uploadOptions,
      },
      options
    );
  }
}