使用包
- 安装minio(当前为minio 3.5.0)
flutter pub add minio
https://pub.dev/packages/minio 需要科学上网工具
- 安装uuid(文件名唯一)
flutter pub add uuid
实现代码
引入插件
import 'package:minio/io.dart';
import 'package:minio/minio.dart';
import 'package:uuid/uuid.dart';
实现逻辑
static Future<String> uploadImage({required String path}) async {
try {
print('image: $path');
// 使用uuid生成资源名称(唯一)
String name = Uuid().v4();
// 获取文件后缀
String suffix = path.substring(path.lastIndexOf("."));
// 连接资源桶
final minio = Minio(
endPoint: MinioConfig.endPoint,
port: MinioConfig.port,
accessKey: MinioConfig.accessKey,
secretKey: MinioConfig.secretKey,
useSSL: MinioConfig.useSSL,
);
// 上传文件
await minio.fPutObject('openim', name + suffix, path);
// 返回上传文件的名称
return "${MinioConfig.url}$name$suffix";
} catch (err) {
print("update image: err: $err");
return "";
}
}
配置文件
// minio配置文件 (不知道的参数找后台要)
class MinioConfig {
static const String endPoint = 'xxx.xxx.xxx.xxx'; // ip
static const int port = 10005; // 端口
static const String accessKey = 'username'; // 用户名
static const String secretKey = 'paseword'; // 密码
static const bool useSSL = false; // 是否开启https
static get url {
return "http://${endPoint}:${port}/openim/"; // 最后使用时,资源的前缀
}
}
- 注意点
path
是其他组件选中的图片路径,如:/data/user/0/com.hisw.im/cache/image_cropper_1667213840911.jpg
endPoint
参数只可以填写ip或域名,不能添加特殊符号(/
也不能有),否则会报错:MinioError: End point 你的配置 is not a valid domain or ip address