前言
minio是一个开源的对象存储服务器,专门设计用于在大规模数据存储环境中运行。它兼容amazon s3 api,因此可以作为一个替代方案用于存储和检索数据,特别是用于云存储和分布式存储场景。
minio的部署非常简单,在官网下载 minio.exe,然后使用批处理命令即可部署,非常方便。
c:/users/admin/desktop/minio.exe server z:/minio/data/
后面的 z:/minio/data/ 代表minio存储的物理位置。
如果需要在c#中对minio库进行操作,需要引入对应的minio库。

官方的示例代码在github上已经有了,建议在官网example中查找对应需要。
这里介绍几个常用的minio在c#中的操作方法
1.建立minio链接实例
public bool getminoconnection(string ip)
{
// "kw5xqbkfmj787ylgescu", "anb72egbqngtdtgke8koad0dbuj1sztgcle81234" 192.168.50.196
// "2mx2wsbgwiklr9efdamg", "1234t0olarccqiqc7ef406kwhtt0ekqreaazelty" 192.168.10.211
try
{
if (ip == "192.168.50.196" || ip == "192.168.50.195")
{
minioclient = new minioclient().withendpoint("192.168.50.196", 9000)
.withcredentials("kw5xqbkfmj787ylgescu", "anb72egbqngtdtgke8koad0dbuj1sztgcle81234")
.build();
}
else if (ip == "192.168.10.211" || ip == "192.168.10.212")
{
minioclient = new minioclient().withendpoint("192.168.10.211", 9000)
.withcredentials("2mx2wsbgwiklr9efdamg", "1234t0olarccqiqc7ef406kwhtt0ekqreaazelty")
.build();
}
}
catch (exception ex)
{
console.out.writeline(ex.message);
return false;
}
return true;
}
2.异步上传文件
①.主要使用流的方式上传文件,内容以字符串为例
需要注意的是,如果上传的文件对应的miniopath地址相同,则文件会直接覆盖,达到更新的作用
public async task<bool> uploaderasync(string ip, string miniopath, string result)
{
if (getminoconnection(ip))
{
await run(minioclient, miniopath, result);
minioclient.dispose();
return true;
}
return false;
}
private async task run(iminioclient minio, string miniopath,string result)
{
var bucketname = "imagestore";
var objectname = miniopath;
var contenttype = "application/text";
byte[] bytearray = encoding.utf8.getbytes(result);
try
{
using (memorystream stream = new memorystream(bytearray))
{
stream.position = 0;
// upload a file to bucket.
var putobjectargs = new putobjectargs()
.withbucket(bucketname)
.withobject(objectname)
.withobjectsize(stream.length)
.withstreamdata(stream);
await minio.putobjectasync(putobjectargs);
}
}
catch (minioexception e)
{
console.writeline("file upload error: {0}", e.message);
}
}
②.使用本地文件的方式上传文件
private async task run(iminioclient minio, string foldername, string filename, string basename)
{
var bucketname = "imagestore";
var objectname = foldername + "/" + filename;
var filepath = basename;
//var contenttype = "application/jpg";
try
{
// upload a file to bucket.
var putobjectargs = new putobjectargs()
.withbucket(bucketname)
.withobject(objectname)
.withfilename(filepath);
//.withcontenttype(contenttype);
await minio.putobjectasync(putobjectargs) ;
minio.dispose();
}
catch (minioexception e)
{
console.writeline("file upload error: {0}", e.message);
}
}
3.下载文件
我这里使用的http请求,你也可以按照官方库 getobject去获取
public async task<bitmapimage> dogetfile(string minioheader, int imgnums, string ppath)
{
var miniopath = ppath.split('/')[2] + "/" + ppath.split('/')[3] + "/images/" + imgnums.tostring() + "_output_image.jpg";
bitmapimage bitmapimage = new bitmapimage();
using (httpclient client = new httpclient())
{
try
{
httpresponsemessage responsemessage = await client.getasync(minioheader + miniopath);
responsemessage.ensuresuccessstatuscode();
byte[] imagebytes = await responsemessage.content.readasbytearrayasync();
using (memorystream memory = new memorystream(imagebytes))
{
bitmapimage.begininit();
bitmapimage.streamsource = memory;
bitmapimage.cacheoption = bitmapcacheoption.onload;
bitmapimage.endinit();
}
}
catch (exception ex)
{
console.writeline(ex.message);
}
}
return bitmapimage;
}
差不多就这些
以上就是c#结合minio实现文件上传存储与更新的详细内容,更多关于c# minio文件上传存储与更新的资料请关注代码网其它相关文章!
发表评论