55 lines
1.6 KiB
Dart
55 lines
1.6 KiB
Dart
import 'dart:async';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:EasyDartModule/base/storage/Storage.dart';
|
|
import 'package:minio/minio.dart';
|
|
|
|
class MinioStorage implements Storage {
|
|
final Minio _minio;
|
|
final StorageConfig _config;
|
|
MinioStorage(this._config)
|
|
: _minio = Minio(
|
|
endPoint: _config.host,
|
|
port: _config.port,
|
|
useSSL: _config.ssl,
|
|
accessKey: _config.accessKey,
|
|
secretKey: _config.secretKey);
|
|
|
|
@override
|
|
Future<void> createBucket(String name) async {
|
|
if (await _minio.bucketExists(name)) {
|
|
return;
|
|
}
|
|
return await _minio.makeBucket(name);
|
|
}
|
|
|
|
@override
|
|
Future<void> removeBucket(String name) async {
|
|
return await _minio.removeBucket(name);
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteObject(String bucketName, String objectName) async {
|
|
return await _minio.removeObject(bucketName, objectName);
|
|
}
|
|
|
|
@override
|
|
Future<Uint8List> getObject(String bucketName, String objectName) async {
|
|
MinioByteStream stream = await _minio.getObject(bucketName, objectName);
|
|
// 将 Stream<List<int>> 转为 List<int>
|
|
final List<int> bytes = await stream
|
|
.toList()
|
|
.then((chunks) => chunks.expand((chunk) => chunk).toList());
|
|
|
|
// 转换为 Uint8List
|
|
return Uint8List.fromList(bytes);
|
|
}
|
|
|
|
@override
|
|
Future<String> uploadObject(
|
|
String bucketName, String objectName, Uint8List data) async {
|
|
await _minio.putObject(bucketName, objectName, Stream.fromIterable([data]));
|
|
return "http${_config.ssl ? "s" : ""}://${_config.host}:${_config.port}/$bucketName/$objectName";
|
|
}
|
|
}
|