首次提交

This commit is contained in:
qmqz
2025-01-02 10:08:03 +08:00
commit fd62ed3d98
16 changed files with 1143 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import 'dart:typed_data';
abstract class Storage {
static late Storage _storage;
static Storage getInstance() {
return _storage;
}
static void setInstance(Storage server) {
_storage = server;
}
Future<void> createBucket(String name);
Future<void> removeBucket(String name);
Future<String> uploadObject(
String bucketName, String objectName, Uint8List data);
Future<Uint8List> getObject(String bucketName, String objectName);
Future<void> deleteObject(String bucketName, String objectName);
}
class StorageConfig {
final String host;
final int port;
final bool ssl;
final String accessKey;
final String secretKey;
StorageConfig(
{required this.host,
required this.port,
this.ssl = false,
required this.accessKey,
required this.secretKey});
}

View File

@@ -0,0 +1,54 @@
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";
}
}