Blame view

lib/utils/aliyun_oss_util.dart 2.7 KB
795acc7e   Key   feat: aliyun oss ...
1
  import 'package:dio/dio.dart';
795acc7e   Key   feat: aliyun oss ...
2
  import 'package:flutter_oss_aliyun/flutter_oss_aliyun.dart';
bc0550ae   Key   fixed: avatar修改
3
  import 'package:wow_english/common/request/api_response/api_response_entity.dart';
795acc7e   Key   feat: aliyun oss ...
4
5
6
7
8
9
10
11
12
  import 'package:wow_english/common/request/request_client.dart';
  import 'package:wow_english/models/aliyun_oss_upload_sts_entity.dart';
  import 'package:wow_english/utils/log_util.dart';
  
  /// 阿里云 oss 工具类,服务端给的鉴权一次有效
  /// 这个库的Client是个单例,如果并发使用请注意所调用的Client归属,Client.init会生成一个新的_instance
  class AliyunOssUtil {
    static Future<String> uploadFile(String filePath) async {
      // 取出文件名
bc0550ae   Key   fixed: avatar修改
13
14
15
      //String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length);
      String fileName = filePath.split('/').last;
      Log.d("待上传文件: [$filePath], 截取的fileName: [$fileName]");
795acc7e   Key   feat: aliyun oss ...
16
      // 获取鉴权信息
b15fde72   liangchengyou   feat:头像上传功能完善逻辑
17
      AliyunOssUploadStsEntity? stsEntity = await _getSts(fileName);
bc0550ae   Key   fixed: avatar修改
18
19
20
      if (stsEntity == null) {
        // 为空的情况抛出错误
        throw 'STS为空';
b15fde72   liangchengyou   feat:头像上传功能完善逻辑
21
      }
bc0550ae   Key   fixed: avatar修改
22
      //Log.d('鉴权信息: $stsEntity');
795acc7e   Key   feat: aliyun oss ...
23
24
25
26
27
28
29
30
      // 鉴权
      Client.init(
        ossEndpoint: stsEntity.endpoint,
        bucketName: stsEntity.bucket,
        authGetter: stsEntity.authGetter,
      );
  
      // 上传文件
bc0550ae   Key   fixed: avatar修改
31
      final Response<dynamic> resp = await Client().putObjectFile(
795acc7e   Key   feat: aliyun oss ...
32
33
34
35
        filePath,
        fileKey: stsEntity.fileKey,
        option: PutRequestOption(
          onSendProgress: (count, total) {
bc0550ae   Key   fixed: avatar修改
36
            Log.d("send: $count/$total");
795acc7e   Key   feat: aliyun oss ...
37
38
          },
          onReceiveProgress: (count, total) {
bc0550ae   Key   fixed: avatar修改
39
            Log.d("receive: $count/$total");
795acc7e   Key   feat: aliyun oss ...
40
          },
bc0550ae   Key   fixed: avatar修改
41
          aclModel: AclMode.inherited,
795acc7e   Key   feat: aliyun oss ...
42
43
44
45
46
47
48
          callback: Callback(
            callbackUrl: stsEntity.callbackParam.callbackUrl,
            callbackBody: stsEntity.callbackParam.callbackBody,
            calbackBodyType: CalbackBodyType.json,
          ),
        ),
      );
bc0550ae   Key   fixed: avatar修改
49
50
51
52
53
54
55
56
57
      var url = '${stsEntity.host}/${stsEntity.fileKey}';
      // 正确返回时,是我们自己服务器的回调,所以使用我们的格式解析
      var result = ApiResponse.fromJson(resp.data);
      Log.d('上传结果: $result, url: $url');
      if (result.code != 200) {
        // 上传失败抛出错误
        throw result.msg ?? '未知错误';
      }
      return url;
795acc7e   Key   feat: aliyun oss ...
58
59
60
    }
  
    /// 获取鉴权信息
b15fde72   liangchengyou   feat:头像上传功能完善逻辑
61
    static Future<AliyunOssUploadStsEntity?> _getSts(String fileName) async {
bc0550ae   Key   fixed: avatar修改
62
63
      var result =
          await requestClient.get<AliyunOssUploadStsEntity>(Apis.aliyunOssSts, queryParameters: {'fileName': fileName});
795acc7e   Key   feat: aliyun oss ...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      return result;
    }
  }
  
  extension StsExtension on AliyunOssUploadStsEntity {
    Auth authGetter() {
      return Auth(
        accessKey: accessKeyId,
        accessSecret: accessKeySecret,
        expire: expiration,
        secureToken: securityToken,
      );
    }
  }