import 'dart:typed_data'; import 'package:flutter_oss_aliyun/flutter_oss_aliyun.dart'; class OSSManager { // 静态单例实例 static final OSSManager _instance = OSSManager._internal(); bool isInit = false; // 工厂构造函数,返回单例实例 factory OSSManager() { return _instance; } // 私有构造函数,防止外部直接实例化 OSSManager._internal(); void init(String stsUrl, String ossEndpoint, String bucketName) { // 初始化 Client.init( stsUrl: stsUrl, ossEndpoint: ossEndpoint, bucketName: bucketName, ); isInit = true; } Future uploadFile(Uint8List fileData, String objectName, {Function(int count, int total)? onSendProgress, Function(int count, int total)? onReceiveProgress}) async { if (!isInit) { throw Exception("请先初始化"); } final result = await Client().putObject(fileData, objectName, option: PutRequestOption( onSendProgress: onSendProgress != null ? (count, total) { onSendProgress(count, total); } : null, onReceiveProgress: onReceiveProgress != null ? (count, total) { onReceiveProgress(count, total); } : null, override: false, aclModel: AclMode.publicRead, storageType: StorageType.ia, ), ); return result.statusCode == 200 ? result.realUri.path : throw Exception("上传失败"); } }