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.
46 lines
1.4 KiB
46 lines
1.4 KiB
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<String> 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("上传失败");
|
|
}
|
|
}
|