upload & save2DB

master
ZGGSONG 3 years ago
parent 51c3c09b42
commit 58d2b56352

@ -13,7 +13,7 @@ namespace Ksat.Supplyment.Library.Model.Uploader
public int Id { get; set; } public int Id { get; set; }
/// <summary> /// <summary>
/// 属于哪个上传任务 /// 属于哪个上传任务,可指代线体
/// </summary> /// </summary>
[Required] [Required]
public string UploaderID { get; set; } public string UploaderID { get; set; }

@ -17,6 +17,9 @@ namespace S3Demo
private static BasicAWSCredentials credentials = new BasicAWSCredentials("admin", "admin123."); private static BasicAWSCredentials credentials = new BasicAWSCredentials("admin", "admin123.");
//private static BasicAWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); //private static BasicAWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
private static string bucketName = "minio/test"; 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> /// <summary>
/// https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Config.html /// https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Config.html
@ -40,13 +43,6 @@ namespace S3Demo
// ForcePathStyle = true // ForcePathStyle = true
//}; //};
// 监控目录
// D:\ClientDir\line01\AOI\20211129\133055851SN001OK.png
private static string[] paths = { @"D:\ClientDir" };
private static string suffix = "*.png";
private static bool incsubdir = true;
public static void Main() public static void Main()
{ {
/// 写入数据库操作 /// 写入数据库操作
@ -64,21 +60,15 @@ namespace S3Demo
// var i = dev.SaveChanges(); // var i = dev.SaveChanges();
// Console.WriteLine(i); // Console.WriteLine(i);
//} //}
try try
{ {
//FileModel.onFileChange(paths, suffix, true);
//await CreateBucket.CreatingBucket("minio/bu29", credentials, conf);
foreach (var path in paths) foreach (var path in paths)
{ {
FileSystemWatcher watch = new FileSystemWatcher() FileSystemWatcher watch = new FileSystemWatcher()
{ {
Path = path, Path = path,
Filter = suffix, Filter = suffix,
IncludeSubdirectories = incsubdir, IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastAccess NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.FileName
@ -137,27 +127,6 @@ namespace S3Demo
//Console.WriteLine("delete bucket " + bucketName); //Console.WriteLine("delete bucket " + bucketName);
*/ */
#endregion #endregion
#region 路径解析
//string fullpath = @"C:\line01\AOI\20211129\120955851P107CN14T00001OK.png";
//string fileName = System.IO.Path.GetFileNameWithoutExtension(fullpath);
//string date = System.IO.Path.GetDirectoryName(fullpath).Split('\\')[3];
//string equipmentName = System.IO.Path.GetDirectoryName(fullpath).Split('\\')[2];
//Console.WriteLine("filename: " + fileName);
//Console.WriteLine("date: " + date);
//Console.WriteLine("equipmentName: " + equipmentName);
////string time = Regex.Match(fileName, @"\d{9}").Value;
//string time = fileName.Substring(0, 9);
//string sn = fileName.Substring(time.Length, 14);
//string code = fileName.Substring(fileName.Length - 2, 2);
//Console.WriteLine("time: " + time);
//Console.WriteLine("sn: " + sn);
//Console.WriteLine("code: " + code);
//string datetime = date + time;
//Console.WriteLine("datetime: " + datetime);
//string dt = DateTime.ParseExact(datetime, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss:fff");
//Console.WriteLine("format: " + dt);
#endregion
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -171,6 +140,9 @@ namespace S3Demo
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
/// <remarks>
/// 主要逻辑
/// </remarks>
private static void OnCreated(object sender, FileSystemEventArgs e) private static void OnCreated(object sender, FileSystemEventArgs e)
{ {
try try
@ -186,15 +158,30 @@ namespace S3Demo
String[] tags = new String[] { sn, dt_fomat, code }; String[] tags = new String[] { sn, dt_fomat, code };
string S3Path = lineName + "/" + equipmentName + "/" + date + "/" + fileName; string S3Path = lineName + "/" + equipmentName + "/" + date + "/" + fileName;
//发送太快会出问题
//没有使用异步线程,可能没有足够的时间等待返回结果 [说明](https://www.cnblogs.com/lnwuyaowei/p/12672866.html) //没有使用异步线程,可能没有足够的时间等待返回结果 [说明](https://www.cnblogs.com/lnwuyaowei/p/12672866.html)
string flag = ObjectModel.Putobjectswithtags(bucketName, S3Path, e.FullPath, tags, credentials, conf).Result; 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); Console.WriteLine("S3对象存储路径: " + bucketName + "/" + S3Path);
Console.WriteLine("上传返回: " + flag);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message.ToString()); Console.WriteLine("Run: " + ex.Message);
} }
} }

Loading…
Cancel
Save