Merge remote-tracking branch 'origin/main'

feature-flyModify
陈伟 2025-06-13 15:13:54 +08:00
commit 5020a26e70
3 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Minio.Exceptions;
using Minio;
using Microsoft.Extensions.Configuration;
using Minio.DataModel.Args;
using Minio.DataModel;
namespace Infrastructure.CloudSdk.minio;
public class MinioService
{
private IMinioClient _minioClient;
public readonly string _bucketName = null;
public MinioService()
{
InitializeMinIOClient();
EnsureBucketExistsAsync(_bucketName).Wait();
}
private void InitializeMinIOClient()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("global.json", optional: false, reloadOnChange: true);
// 构建配置
var config = builder.Build();
_minioClient = new MinioClient()
.WithEndpoint(config["Minio:Endpoint"])
.WithCredentials(config["Minio:AccessKey"], config["Minio:SecretKey"])
.Build();
}
/// <summary>
/// 创建存储桶(如果不存在)
/// </summary>
public async Task CreateBucketIfNotExists(string _bucketName)
{
var existsArgs = new BucketExistsArgs().WithBucket(_bucketName);
bool found = await _minioClient.BucketExistsAsync(existsArgs);
if (!found)
{
var makeArgs = new MakeBucketArgs().WithBucket(_bucketName);
await _minioClient.MakeBucketAsync(makeArgs);
Console.WriteLine($"Bucket {_bucketName} created.");
}
}
public async Task EnsureBucketExistsAsync(string bucketName)
{
var existsArgs = new BucketExistsArgs().WithBucket(bucketName);
var x = await _minioClient.BucketExistsAsync(existsArgs);
Console.WriteLine($" {bucketName} exist status: " + x);
// 如果存储桶不存在,则创建存储桶
if (!x)
{
var makeArgs = new MakeBucketArgs().WithBucket(bucketName);
await _minioClient.MakeBucketAsync(makeArgs);
}
}
/// <summary>
/// 下载文件
/// </summary>
public async Task DownLoadObject(string bucketName, string objectKey, string localDir, string objectETag,
CancellationToken token = default)
{
var index = objectKey.LastIndexOf("/", StringComparison.Ordinal);
if (index > 0)
{
var dir = Path.Combine(localDir, objectKey.Substring(0, index));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
var localPath = Path.Combine(localDir, objectKey.Replace('/', Path.DirectorySeparatorChar));
var getArgs = new GetObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
.WithObject(objectKey)
.WithFile(localPath);
var stat = await _minioClient.GetObjectAsync(getArgs, token);
}
/// <summary>
/// 列出所有对象
/// </summary>
public async Task<IAsyncEnumerable<Item>> ListAllObject(string bucketName, string prefix, bool recursive,
CancellationToken token = default)
{
// Just list of objects
// Check whether 'mybucket' exists or not.
if (string.IsNullOrEmpty(bucketName))
{
bucketName = _bucketName;
}
var existsArgs = new BucketExistsArgs().WithBucket(bucketName);
var found = await _minioClient.BucketExistsAsync(existsArgs, token);
if (found)
{
var args = new ListObjectsArgs()
.WithBucket(bucketName)
.WithPrefix(prefix)
.WithRecursive(recursive);
return _minioClient.ListObjectsEnumAsync(args, token);
}
Console.WriteLine("mybucket does not exist");
throw new Exception("bucket not found");
}
/// <summary>
/// 删除文件
/// </summary>
public async Task DeleteFile(string objectName)
{
try
{
var deleteargs = new RemoveObjectArgs().WithBucket(_bucketName).WithObject(objectName);
await _minioClient.RemoveObjectAsync(deleteargs);
Console.WriteLine($"File {objectName} deleted.");
}
catch (MinioException ex)
{
Console.WriteLine($"MinIO Exception: {ex.Message}");
}
}
}

View File

@ -17,6 +17,7 @@
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Minio" Version="6.0.4" />
<PackageReference Include="MQTTnet" Version="4.1.4.563" />
<PackageReference Include="NetTopologySuite" Version="2.5.0" />
<PackageReference Include="NetTopologySuite.IO.Esri.Shapefile" Version="1.0.0" />

View File

@ -0,0 +1,33 @@
//{
// "Minio": {
// "Endpoint": "192.168.20.239:9000",
// "AccessKey": "cqlatqb0dgqdBRar7KVF",
// "SecretKey": "FUFzd5VeoBeWhrpql6MBON5tQxNzcIgaK7vkvofX",
// "BucketName": "drone"
// }
//}
{
"Minio": {
"Endpoint": "192.168.10.163:9016",
"AccessKey": "I2c35jD6ayApaneyQZyC",
"SecretKey": "XHlrNeCHK0xf8y2Fo0K5OKyDeaI2ItfEsFbzQPFk",
"BucketName": "demo",
"limitspeed": 1048576
}
//{
// "Minio": {
// "Endpoint": "192.168.20.239:9106",
// "AccessKey": "minioadmin",
// "SecretKey": "hopetry@minio",
// "BucketName": "dev",
// "limitspeed": 1048576
// }
//"Minio": {
// "Endpoint": "nas.continue.fun:10044",
// "AccessKey": "xilonp6jAjBIf0DTaLfY",
// "SecretKey": "DgJF1eUTy61V3s26ofzfpRKUsnr9YVAF5kkwYXZ3",
// "BucketName": "mdimage",
// "limitspeed": 1048576
//}
}