A Simple Aliyun OSS Upload for Flutter.
- Add this to your pubspec.yaml file:
dependencies:
better_aliyun_oss: ^0.0.8
- Install it
$ flutter packages get
late BetterAliyunOssClient ossClient;
late StreamSubscription eventStreamSubscription;
@override
void initState() {
super.initState();
ossClient = BetterAliyunOssClient(credentials);
eventStreamSubscription = ossClient.eventStream.listen((event) {
print("$event");
});
}
Future<BetterAliyunOssCredentials?> credentials() async {
final credentials = {
"SecurityToken": "",
"AccessKeyId": "",
"AccessKeySecret": "",
"Expiration": "",
};
return BetterAliyunOssCredentials.fromMap(credentials);
}
void uploading() async {
final objectFileName = Uuid().v1().replaceAll("-", "") + path.extension(file.path);
final objectPath = "image/${DateFormat("yyyyMM").format(DateTime.now())}/$objectFileName";
simplePutRequest = ossClient.putObject(
bucket: () async => "my-bucket",
endpoint: () async => "oss-cn-hangzhou.aliyuncs.com",
domain: () async => "https://domain.com",
objectPath: objectPath,
contentType: lookupMimeType(file.path) ?? "application/octet-stream",
path: file.path,
);
print("简单数据上传请求 id : ${simplePutRequest.requestTaskId}");
}
void cancelSimpleUpload() {
if (simplePutRequest != null) {
ossClient.cancelPutObject(simplePutRequest!);
}
}
- simple upload file to Aliyun OSS.
- multipart upload file to Aliyun OSS.