|
|
|
|
using Amazon;
|
|
|
|
|
using Amazon.Runtime;
|
|
|
|
|
using Amazon.S3;
|
|
|
|
|
using System;
|
|
|
|
|
using S3Demo.Storage;
|
|
|
|
|
using S3Demo.Model;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Ksat.Supplyment.Library.Model;
|
|
|
|
|
|
|
|
|
|
namespace S3Demo
|
|
|
|
|
{
|
|
|
|
|
public class Run
|
|
|
|
|
{
|
|
|
|
|
private static BasicAWSCredentials credentials = new BasicAWSCredentials("admin", "admin123.");
|
|
|
|
|
//private static BasicAWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
|
|
|
|
|
private static string bucketName = "minio/test";
|
|
|
|
|
// 受监控的目录,完整目录如:"D:\ClientDir\line01\AOI\20211129\133055851SN001OK.png"
|
|
|
|
|
private static string[] paths = { @"D:\ClientDir" };
|
|
|
|
|
private static string suffix = "*.png";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Config.html
|
|
|
|
|
/// https://blog.csdn.net/tw_tangliang/article/details/118669099
|
|
|
|
|
/// https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/dotnetv3/S3/UploadFileMPUHighLevelAPIExample/UploadFileMPUHighLevelAPIExample/UploadFileMPUHighLevelAPI.cs
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
// 通过IP + Port方式
|
|
|
|
|
private static AmazonS3Config conf = new AmazonS3Config()
|
|
|
|
|
{
|
|
|
|
|
UseHttp = true,
|
|
|
|
|
ProxyHost = "http://192.168.60.132",
|
|
|
|
|
ProxyPort = 9000,
|
|
|
|
|
RegionEndpoint = RegionEndpoint.USEast1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//通过固定域名的方式
|
|
|
|
|
//private static AmazonS3Config conf = new AmazonS3Config()
|
|
|
|
|
//{
|
|
|
|
|
// ServiceURL = "https://play.min.io",
|
|
|
|
|
// ForcePathStyle = true
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
/// 写入数据库操作
|
|
|
|
|
//using (var dev = new CodeFirstDbContext())
|
|
|
|
|
//{
|
|
|
|
|
// dev.UploadCaches.Add(new Ksat.Supplyment.Library.Model.Uploader.UploadCache() {
|
|
|
|
|
// Id = 1,
|
|
|
|
|
// UploaderID = "02",
|
|
|
|
|
// CreateAt = DateTime.Now.AddHours(-1),
|
|
|
|
|
// RetryAt = DateTime.Now,
|
|
|
|
|
// RetryCount = 1,
|
|
|
|
|
// ErrorInfo = "null",
|
|
|
|
|
// RequestData = "testInfo"
|
|
|
|
|
// });
|
|
|
|
|
// var i = dev.SaveChanges();
|
|
|
|
|
// Console.WriteLine(i);
|
|
|
|
|
//}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
|
|
|
|
FileSystemWatcher watch = new FileSystemWatcher()
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
Filter = suffix,
|
|
|
|
|
IncludeSubdirectories = true,
|
|
|
|
|
NotifyFilter = NotifyFilters.LastAccess
|
|
|
|
|
| NotifyFilters.LastWrite
|
|
|
|
|
| NotifyFilters.FileName
|
|
|
|
|
| NotifyFilters.DirectoryName,
|
|
|
|
|
};
|
|
|
|
|
watch.Created += new FileSystemEventHandler(OnCreated);
|
|
|
|
|
watch.Error += OnError;
|
|
|
|
|
// 开始监控
|
|
|
|
|
watch.EnableRaisingEvents = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region S3相关测试
|
|
|
|
|
/*
|
|
|
|
|
//creating Bucket
|
|
|
|
|
//await CreateBucket.CreatingBucket("minio/bu29", credentials, conf);
|
|
|
|
|
|
|
|
|
|
//upload Dir
|
|
|
|
|
string dirPath = @"C:\Users\song\Pictures\Saved Pictures\";
|
|
|
|
|
string suffix = "*.png";
|
|
|
|
|
//await UploadDirMPUHighLevelAPI.Uploaderdir(bucketName, dirPath, suffix, credentials, conf);
|
|
|
|
|
|
|
|
|
|
//upload File
|
|
|
|
|
string keyName = "logo.png";
|
|
|
|
|
//await UploadFileMPUHighLevelAPI.Uploaderfile(bucketName, keyName, dirPath, credentials, conf);
|
|
|
|
|
|
|
|
|
|
//list Objects
|
|
|
|
|
//await ListObjects.Listingobjects(bucketName, credentials, conf);
|
|
|
|
|
|
|
|
|
|
//upload Object With Tag
|
|
|
|
|
string filaPath = @"C:\Users\song\Pictures\Saved Pictures\" + keyName;
|
|
|
|
|
String[] tags = new String[] { "SN123456", DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:sszzz"), "AB023" };
|
|
|
|
|
//await ObjectModel.Putobjectswithtags(bucketName, keyName, filaPath, tags, credentials, conf);
|
|
|
|
|
|
|
|
|
|
//read Object
|
|
|
|
|
//await ObjectModel.Readobjectdata(bucketName, keyName, credentials, conf);
|
|
|
|
|
|
|
|
|
|
//isExist Bucket
|
|
|
|
|
//从"minio/test"中取出存储桶名
|
|
|
|
|
var _bucketName = bucketName.Split('/')[1];
|
|
|
|
|
//此处传入bucketName: "test"
|
|
|
|
|
//var flag = await BucketModel.isExistBucket(_bucketName, credentials, conf);
|
|
|
|
|
//if (flag)
|
|
|
|
|
//{
|
|
|
|
|
// Console.WriteLine("exist");
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// Console.WriteLine("not exist");
|
|
|
|
|
// //docker创建buckeyt时此处传入BucketName: "minio/test"
|
|
|
|
|
// //play.min.io创建buckeyt时传入BucketName: "test"
|
|
|
|
|
// await BucketModel.CreateBucket(bucketName, credentials, conf);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
////删除桶:docker删除桶"minio/test"; play.min.io删除桶"test"
|
|
|
|
|
//await new AmazonS3Client(credentials, conf).DeleteBucketAsync(bucketName);
|
|
|
|
|
//Console.WriteLine("delete bucket " + bucketName);
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 监控到创建文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 主要逻辑
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private static void OnCreated(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string lineName = Path.GetDirectoryName(e.FullPath).Split('\\')[2];
|
|
|
|
|
string equipmentName = Path.GetDirectoryName(e.FullPath).Split('\\')[3];
|
|
|
|
|
string date = Path.GetDirectoryName(e.FullPath).Split('\\')[4];
|
|
|
|
|
string fileName = Path.GetFileName(e.FullPath);
|
|
|
|
|
string datetime = date + fileName.Substring(0, 9);
|
|
|
|
|
string dt_fomat = DateTime.ParseExact(datetime, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss:fff");
|
|
|
|
|
string code = fileName.Substring(fileName.Length - 6, 2);
|
|
|
|
|
string sn = fileName.Substring(9, 14);
|
|
|
|
|
String[] tags = new String[] { sn, dt_fomat, code };
|
|
|
|
|
string S3Path = lineName + "/" + equipmentName + "/" + date + "/" + fileName;
|
|
|
|
|
|
|
|
|
|
//发送太快会出问题
|
|
|
|
|
//没有使用异步线程,可能没有足够的时间等待返回结果 [说明](https://www.cnblogs.com/lnwuyaowei/p/12672866.html)
|
|
|
|
|
string flag = ObjectModel.Putobjectswithtags(bucketName, S3Path, e.FullPath, tags, credentials, conf).Result;
|
|
|
|
|
if (flag.Equals("OK"))
|
|
|
|
|
{
|
|
|
|
|
using (var dev = new CodeFirstDbContext())
|
|
|
|
|
{
|
|
|
|
|
dev.UploadFinishs.Add(new Ksat.Supplyment.Library.Model.Uploader.UploadFinish()
|
|
|
|
|
{
|
|
|
|
|
UploaderID = lineName,
|
|
|
|
|
CreateAt = DateTime.ParseExact(datetime, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture),
|
|
|
|
|
RetryAt = DateTime.Now,
|
|
|
|
|
RetryCount = 1,
|
|
|
|
|
RequestData = S3Path
|
|
|
|
|
});
|
|
|
|
|
string res = dev.SaveChanges() == 1 ? "上传并保存成功" : "上传成功,保存失败";
|
|
|
|
|
Console.WriteLine(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("S3对象存储路径: " + bucketName + "/" + S3Path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Run: " + ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 出现错误
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private static void OnError(object sender, ErrorEventArgs e) =>
|
|
|
|
|
PrintException(e.GetException());
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打印错误
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
private static void PrintException(Exception? ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Message: {ex.Message}");
|
|
|
|
|
Console.WriteLine("Stacktrace:");
|
|
|
|
|
Console.WriteLine(ex.StackTrace);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
PrintException(ex.InnerException);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|