THupload-Oss
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

422 lines
18KB

  1. using Aliyun.OSS;
  2. using System;
  3. using log4net;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Reflection;
  9. using System.IO;
  10. using static THUploadOss.Common;
  11. using Newtonsoft.Json.Linq;
  12. using Aliyun.OSS.Common;
  13. using System.Runtime.InteropServices;
  14. using System.Runtime.InteropServices.ComTypes;
  15. using System.Security.Policy;
  16. namespace THUploadOss
  17. {
  18. public class THUploadFile
  19. {
  20. public string m_accessKeyId;
  21. public string m_accessKeySecret;
  22. public string m_endpoint;
  23. public string m_bucketName;
  24. public string m_objectName;
  25. public string m_localPicPath;
  26. public string m_localVideoPath;
  27. private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  28. public static float m_uploadProgressAdd = 0;
  29. public static OssClient client = null;
  30. int partSize = 1024 * 1024; //分片上传大小
  31. public bool InitUploadFile()
  32. {
  33. if (!getMqttConfig())
  34. return false;
  35. return true;
  36. }
  37. public void UploadFile()
  38. {
  39. if (DownloadFinsh == 1)
  40. {
  41. client = new OssClient(m_endpoint, m_accessKeyId, m_accessKeySecret);
  42. string[] files1 = Directory.GetFiles(m_localVideoPath + RequestId);
  43. string[] files2 = Directory.GetFiles(m_localPicPath + RequestId);
  44. string[] files = files1.Concat(files2).ToArray();
  45. lock (lockFileList)
  46. {
  47. for(int i= FileList.Count-1; i>=0 ; i--)
  48. {
  49. FileList.Remove(FileList[i]);
  50. }
  51. foreach (string file in files1)
  52. { // 设置URL过期时间为1小时
  53. FileStates fileState = new FileStates();
  54. int lastIndex = file.LastIndexOf('\\');
  55. fileState.fileName = file.Substring(lastIndex + 1);
  56. string[] endpoint = m_endpoint.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  57. string url = "https://" + m_bucketName + "." + endpoint[endpoint.Length - 1] + "/" + RequestId + "/" + fileState.fileName;
  58. fileState.url = url; //"oss://" +m_bucketName+"/"+ RequestId + "/" + fileState.fileName;
  59. fileState.status = 5;
  60. fileState.progress = "0.0000";
  61. fileState.fileType = "video";
  62. FileList.Add(fileState);
  63. }
  64. foreach (string file in files2)
  65. {
  66. FileStates fileState = new FileStates();
  67. int lastIndex = file.LastIndexOf('\\');
  68. fileState.fileName = file.Substring(lastIndex + 1);
  69. string[] endpoint = m_endpoint.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  70. string url = "https://" + m_bucketName + "." + endpoint[endpoint.Length - 1] + "/" + RequestId + "/" + fileState.fileName;
  71. fileState.url = url;
  72. fileState.status = 5;
  73. fileState.progress = "0.0000";
  74. fileState.fileType = "pic";
  75. FileList.Add(fileState);
  76. }
  77. }
  78. int index = 0;
  79. foreach (string file in files)
  80. {
  81. FileInfo fileinfo = new FileInfo(file);
  82. string[] result = file.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  83. string name = result[result.Length - 1];
  84. string objectPath = m_objectName + RequestId + "/" + name;
  85. if (fileinfo.Length == 0)
  86. {
  87. lock (lockFileList)
  88. {
  89. FileList.Insert(index, new FileStates()
  90. {
  91. fileName = FileList[index].fileName,
  92. fileType = FileList[index].fileType,
  93. url = FileList[index].url,
  94. status = 15,
  95. progress = "1.0000",
  96. });
  97. FileList.Remove(FileList[index + 1]);
  98. }
  99. index++;
  100. continue;
  101. }
  102. if (StsrtDownload == -1)
  103. {
  104. Status = (int)UPLOAD_STATUS.Ccancellation;
  105. return ;
  106. }
  107. try
  108. {
  109. var uploadId = InitiateMultipartUpload(this.m_bucketName, objectPath);
  110. var partETags = UploadParts(this.m_bucketName, objectPath, file, uploadId, partSize, index);
  111. if(partETags != null)
  112. {
  113. CompleteUploadPart(this.m_bucketName, objectPath, uploadId, partETags);
  114. bool doesexist = client.DoesObjectExist(this.m_bucketName, objectPath);
  115. if (doesexist)
  116. {
  117. lock (lockFileList)
  118. {
  119. FileList.Insert(index, new FileStates()
  120. {
  121. fileName = FileList[index].fileName,
  122. fileType = FileList[index].fileType,
  123. url = FileList[index].url,
  124. status = 15,
  125. progress = "1.0000",
  126. });
  127. FileList.Remove(FileList[index + 1]);
  128. }
  129. Logger.WriteInfo("THUploadFile: " + FileList[index].fileName + " UploadFile-上传成功。");
  130. }
  131. else
  132. {
  133. lock (lockFileList)
  134. {
  135. FileList.Insert(index, new FileStates()
  136. {
  137. fileName = FileList[index].fileName,
  138. fileType = FileList[index].fileType,
  139. url = FileList[index].url,
  140. status = 20,
  141. progress = (UploadPercent).ToString(),
  142. });
  143. FileList.Remove(FileList[index + 1]);
  144. }
  145. Logger.WriteError("THUploadFile: " + FileList[index].fileName + " UploadFile-上传失败。");
  146. }
  147. }
  148. }
  149. catch (OssException ex)
  150. {
  151. lock (lockFileList)
  152. {
  153. FileList.Insert(index, new FileStates()
  154. {
  155. fileName = FileList[index].fileName,
  156. fileType = FileList[index].fileType,
  157. url = FileList[index].url,
  158. status = 20,
  159. progress = (UploadPercent).ToString(),
  160. });
  161. FileList.Remove(FileList[index + 1]);
  162. }
  163. Logger.WriteError("THUploadFile: " + FileList[index].fileName + " UploadFile-上传失败。" + ex.Message + "。");
  164. }
  165. index++;
  166. }
  167. DownloadFinsh = -1;
  168. StsrtDownload = -1;
  169. Status = (int)UPLOAD_STATUS.Complete;
  170. }
  171. else
  172. System.Threading.Thread.Sleep(1000);
  173. }
  174. private static string InitiateMultipartUpload(String bucketName, String objectName)
  175. {
  176. try
  177. {
  178. var request = new InitiateMultipartUploadRequest(bucketName, objectName);
  179. var result = client.InitiateMultipartUpload(request);
  180. return result.UploadId;
  181. }
  182. catch
  183. {
  184. Logger.WriteError("THUploadFile: InitiateMultipartUpload-初始化失败");
  185. }
  186. return null;
  187. }
  188. private static List<PartETag> UploadParts(String bucketName, String objectName, String fileToUpload, String uploadId, int partSize,int index)
  189. {
  190. m_uploadProgressAdd = 0;
  191. var fi = new FileInfo(fileToUpload);
  192. var fileSize = fi.Length;//单个文件大小
  193. var partCount = fileSize / partSize;
  194. if (fileSize % partSize != 0)
  195. {
  196. partCount++;
  197. }
  198. var partETags = new List<PartETag>();
  199. try
  200. {
  201. using (var fs = File.Open(fileToUpload, FileMode.Open))
  202. {
  203. for (var i = 0; i < partCount; i++)
  204. {
  205. var skipBytes = (long)partSize * i;
  206. fs.Seek(skipBytes, 0);
  207. var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
  208. m_uploadProgressAdd += size;
  209. UploadPercent = Math.Round((Double)m_uploadProgressAdd / (Double)fileSize, 4);
  210. Logger.WriteInfo("THUploadFile: UploadParts-UploadPercent= " + UploadPercent);
  211. if (StsrtDownload == -1)
  212. {
  213. Status = (int)UPLOAD_STATUS.Ccancellation;
  214. return null;
  215. }
  216. lock (lockFileList) {
  217. FileList.Insert(index, new FileStates()
  218. {
  219. fileName = FileList[index].fileName,
  220. fileType = FileList[index].fileType,
  221. url = FileList[index].url,
  222. status = 10,
  223. progress = (UploadPercent).ToString(),
  224. });
  225. FileList.Remove(FileList[index + 1]);
  226. }
  227. //FileList[].progress = (UploadPercent).ToString();
  228. var request = new UploadPartRequest(bucketName, objectName, uploadId)
  229. {
  230. InputStream = fs,
  231. PartSize = size,
  232. PartNumber = i + 1
  233. };
  234. var result = client.UploadPart(request);
  235. partETags.Add(result.PartETag);
  236. }
  237. }
  238. }
  239. catch (Exception ex)
  240. {
  241. lock (lockFileList)
  242. {
  243. FileList.Insert(index, new FileStates()
  244. {
  245. fileName = FileList[index].fileName,
  246. fileType = FileList[index].fileType,
  247. url = FileList[index].url,
  248. status = 20,
  249. progress = (UploadPercent).ToString(),
  250. });
  251. FileList.Remove(FileList[index + 1]);
  252. }
  253. Logger.WriteError(ex.ToString());
  254. }
  255. return partETags;
  256. }
  257. private static CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName, String uploadId, List<PartETag> partETags)
  258. {
  259. try
  260. {
  261. var completeMultipartUploadRequest =
  262. new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);
  263. foreach (var partETag in partETags)
  264. {
  265. completeMultipartUploadRequest.PartETags.Add(partETag);
  266. }
  267. return client.CompleteMultipartUpload(completeMultipartUploadRequest);
  268. }catch (Exception ex)
  269. {
  270. Logger.WriteError(ex.ToString());
  271. }
  272. return null;
  273. }
  274. //实现删除指定文件夹下的所有文件
  275. public bool RemoveAllFile(string path)
  276. {
  277. try
  278. {
  279. Directory.Delete(path, true);
  280. Logger.WriteInfo("THUploadFile: RemoveAllFile(" + path + ")-操作完成");
  281. return true;
  282. }
  283. catch (Exception ex)
  284. {
  285. Logger.WriteError("THUploadFile: RemoveAllFile(" + path + ")-发生异常:" + ex.Message);
  286. return false;
  287. }
  288. }
  289. public bool getMqttConfig()
  290. {
  291. if (System.IO.File.Exists(ConfigPath))
  292. {
  293. try
  294. {
  295. StringBuilder accessKeyId = new StringBuilder(200);
  296. GetPrivateProfileString("ALIYUN", "accessKeyId", "配置文件存在,读取未成功!", accessKeyId, 255, ConfigPath);
  297. m_accessKeyId = accessKeyId.ToString();
  298. StringBuilder accessKeySecret = new StringBuilder(200);
  299. GetPrivateProfileString("ALIYUN", "accessKeySecret", "配置文件存在,读取未成功!", accessKeySecret, 255, ConfigPath);
  300. m_accessKeySecret = accessKeySecret.ToString();
  301. StringBuilder endpoint = new StringBuilder(200);
  302. GetPrivateProfileString("ALIYUN", "endpoint", "配置文件存在,读取未成功!", endpoint, 255, ConfigPath);
  303. m_endpoint = endpoint.ToString();
  304. StringBuilder bucketName = new StringBuilder(200);
  305. GetPrivateProfileString("ALIYUN", "bucketName", "配置文件存在,读取未成功!", bucketName, 255, ConfigPath);
  306. m_bucketName = bucketName.ToString();
  307. StringBuilder objectName = new StringBuilder(200);
  308. GetPrivateProfileString("ALIYUN", "objectName", "配置文件存在,读取未成功!", objectName, 255, ConfigPath);
  309. m_objectName = objectName.ToString();
  310. StringBuilder localPicPath = new StringBuilder(10);
  311. GetPrivateProfileString("SMBPATH", "localPicPath", "配置文件存在,读取未成功!", localPicPath, 255, ConfigPath);
  312. m_localPicPath = localPicPath.ToString();
  313. StringBuilder localVideoPath = new StringBuilder(10);
  314. GetPrivateProfileString("SMBPATH", "localVideoPath", "配置文件存在,读取未成功!", localVideoPath, 255, ConfigPath);
  315. m_localVideoPath = localVideoPath.ToString();
  316. return true;
  317. }
  318. catch (Exception ex)
  319. {
  320. Logger.WriteError("THUploadFile: "+ ex.Message.ToString());
  321. return false;
  322. }
  323. }
  324. return false;
  325. }
  326. }
  327. }
  328. //public int UploadFile()
  329. //{
  330. // if (DownloadFinsh == 1)
  331. // {
  332. // string[] files = Directory.GetFiles(m_localPath + RequestId);
  333. // long fileSize = 0;
  334. // foreach (string file in files)
  335. // {
  336. // FileInfo fileinfo = new FileInfo(file);
  337. // fileSize += fileinfo.Length;
  338. // }
  339. // foreach (string file in files)
  340. // {
  341. // FileInfo fileinfo = new FileInfo(file);
  342. // long len = fileinfo.Length;
  343. // float process = 0;
  344. // Console.WriteLine(file);
  345. // string[] result = file.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
  346. // string name = result[result.Length - 1];
  347. // Console.WriteLine(name);
  348. // string objectPath = m_objectName;
  349. // objectPath = objectPath + RequestId + "/" + name;
  350. // var fs = File.Open(file, FileMode.Open);
  351. // var client = new OssClient(m_endpoint, m_accessKeyId, m_accessKeySecret);
  352. // PutObjectRequest putObjectRequest = new PutObjectRequest(m_bucketName, objectPath, fs);
  353. // putObjectRequest.StreamTransferProgress += (obg, args) =>
  354. // {
  355. // float putProcess = (args.TransferredBytes * 100 / args.TotalBytes) / 100.0f;
  356. // // action.Invoke(putProcess);
  357. // if (putProcess >= 1)
  358. // {
  359. // // complete?.Invoke();
  360. // putObjectRequest.StreamTransferProgress = null;
  361. // }
  362. // process = (putProcess * len) / fileSize;
  363. // if ((m_uploadProgressAdd + putProcess) >= 0.01)
  364. // {
  365. // if (putProcess == 1)
  366. // {
  367. // m_uploadProgressAdd += process;
  368. // UploadPercent = m_uploadProgressAdd * 100;
  369. // }
  370. // else
  371. // {
  372. // UploadPercent = (m_uploadProgressAdd + process) *100;
  373. // }
  374. // Console.WriteLine(UploadPercent.ToString());
  375. // }
  376. // };
  377. // try
  378. // {
  379. // client.PutObject(putObjectRequest);
  380. // } catch (Exception ex)
  381. // {
  382. // Console.WriteLine(ex.Message);
  383. // return -1;
  384. // }
  385. // }
  386. // DownloadFinsh = 0;
  387. // UploadFinsh = 1;
  388. // Status = (int)UPLOAD_STATUS.Complete;
  389. // return 0;
  390. // }
  391. // return -1;
  392. //}