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 createBucket(String name) async { if (await _minio.bucketExists(name)) { return; } return await _minio.makeBucket(name); } @override Future removeBucket(String name) async { return await _minio.removeBucket(name); } @override Future deleteObject(String bucketName, String objectName) async { return await _minio.removeObject(bucketName, objectName); } @override Future getObject(String bucketName, String objectName) async { MinioByteStream stream = await _minio.getObject(bucketName, objectName); // 将 Stream> 转为 List final List bytes = await stream .toList() .then((chunks) => chunks.expand((chunk) => chunk).toList()); // 转换为 Uint8List return Uint8List.fromList(bytes); } @override Future uploadObject( String bucketName, String objectName, Uint8List data) async { await _minio.putObject(bucketName, objectName, Stream.fromIterable([data])); return "${_config.path ?? 'http${_config.ssl ? "s" : ""}://${_config.host}:${_config.port}'}/$bucketName/$objectName"; } }