private static void TemplateTransfer(BosOpenStorageService bos, AliyunOpenStorageService aos) { Console.WriteLine("目前程序支持阿里云迁移到百度云"); int siteid = 0, targetSiteID = 0; #region 解析siteid while (true) { Console.WriteLine("请输入阿里云的SiteID"); var sourceSiteID = Console.ReadLine(); if (int.TryParse(sourceSiteID, out siteid)) { break; } } while (true) { Console.WriteLine("请输入百度云的SiteID"); var baiduyunsiteid = Console.ReadLine(); if (int.TryParse(baiduyunsiteid, out targetSiteID)) { break; } } //Console.WriteLine("解析siteid中......"); //var isError = true; //while (isError) //{ // Console.WriteLine("请输入源模板的二级域名, 格式为 889117024.wezhan.cn"); // var domain = Console.ReadLine(); // try // { // domain = "http://" + domain + "/home/index"; // var htmls = RequestUtility.HttpGet(domain, null); // string pattern = "content/sitefiles/\\d{2,7}/images"; // System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); // var temp = re.Match(htmls); // siteid = int.Parse(temp.Value.Replace("content/sitefiles/", "").Replace("/images", "")); // Console.WriteLine("解析完毕, 即将列出所有的问题......"); // isError = false;//跳出循环 // } // catch (Exception ex) // { // Console.WriteLine("出现错误, {0}", ex.Message); // //Console.WriteLine("程序正在尝试二次解析siteid"); // isError = true; // } //} #endregion var list = new List <string>(); //获取云端所有文件 var uplistkey = new List <string>(); //list副本, 上传的时候读取 var uplistFullName = new List <string>(); #region Console.WriteLine("开始从阿里云读取文件"); System.Threading.Thread.Sleep(1000); var index = 1; Aliyun.OpenServices.OpenStorageService.ObjectListing result = null; string nextMarker = string.Empty; do { var listObjectsRequest = new Aliyun.OpenServices.OpenStorageService.ListObjectsRequest(aos.OssConfig.BucketName) { Marker = nextMarker, MaxKeys = 100 , Prefix = "content/sitefiles/" + siteid.ToString() + "/"//只有图片的话不需要sitefiles分组 }; result = aos.ListObjects(listObjectsRequest); Console.WriteLine("File:"); foreach (var summary in result.ObjectSummaries) { var tempkey = summary.Key; var tempkeys = tempkey.Split('/'); var filename = tempkeys[tempkeys.Length - 1]; if (filename.Split('.').Length != 2) //如果没有后缀名. 那就不管了 { continue; } list.Add(summary.Key); Console.WriteLine("{1}--Name:{0}", summary.Key, index); index++; } nextMarker = result.NextMarker; } while (result.IsTruncated);//(false);// Console.WriteLine("\n\n 共{0}个文件, 现在开始下载...", list.Count); uplistkey = list.ToArray().ToList(); #endregion #region 载到本地 目标是程序所在文件夹 //var errorlist = new List<string>(); //var latestError = ""; //for (int i = 0; i < list.Count; i++) //{ // try // { // using (Aliyun.OpenServices.OpenStorageService.OssObject obj = aos.GetObject(aos.OssConfig.BucketName, list[i])) // { // CreateFile(obj); // Console.WriteLine("{0}下载成功--{1}", i + 1, obj.Key); // } // }catch(Exception ex){ // errorlist.Add(list[i]); // latestError = ex.Message; // } //} System.Threading.Thread.Sleep(2000); index = 1; while (list.Count > 0) { var curr = list[0]; try { using (Aliyun.OpenServices.OpenStorageService.OssObject obj = aos.GetObject(aos.OssConfig.BucketName, curr)) { uplistFullName.Add(CreateFile(obj)); Console.WriteLine("{0}下载成功--{1}", index, obj.Key); index++; list.Remove(curr); } } catch (Exception ex) { Console.WriteLine("出现错误, 信息是{0}", ex.Message); Console.WriteLine("重试中......"); } } Console.WriteLine("下载完成"); #endregion Console.WriteLine("开始上传到百度云\n\n"); #region 到百度云 index = 1; while (uplistkey.Count > 0) { var sourceKey = uplistkey[0]; //下载到本地的keys var targetKey = uplistkey[0]; //上传到百度的keys #region 处理key var keys = targetKey.Split('/'); keys[2] = targetSiteID.ToString(); targetKey = ""; keys.ToList().ForEach(x => targetKey += x + "/"); targetKey = targetKey.Remove(targetKey.Length - 1); #endregion //var fullname = Environment.CurrentDirectory + "/" + key; using (FileStream fs = new FileStream(sourceKey, FileMode.Open)) { //获得后缀名 var temp = targetKey.Split('.'); string ext = temp.Length > 1 ? temp[temp.Length - 1] : ""; try { ObjectMetadata metadata = new ObjectMetadata(); metadata.ContentLength = fs.Length; metadata.ContentType = FileContentType.GetMimeType(ext); bos.PutObject(bos.OssConfig.BucketName, targetKey, fs, metadata); Console.WriteLine("完成上传{0}----{1}", index, targetKey); File.Delete(sourceKey);//成功之后就删除本地sourcekey uplistkey.Remove(sourceKey); index++; } catch (Exception ex) { Console.WriteLine("处理{0}遇到问题, 代码是{1}, 当前进度{2}/{3}", sourceKey, ex.Message, index, uplistkey.Count); Console.WriteLine("重试中"); } } } #endregion Console.WriteLine("\n\n完成上传"); Console.ReadLine(); }
private static DateTime RunUpload(DateTime updateDate, string FolderPath, string Pre_key, BosOpenStorageService bos) { #region 读取用户输入 bool flag = true; while (flag) { Console.WriteLine("使用默认更新的时间点 {0} 则输入y, 或者输入时间,格式为:2015-12-05", updateDate.ToString("yyyy-MM-dd")); string P_time = Console.ReadLine(); if (P_time.ToLower() != "y") { DateTime temptime; if (DateTime.TryParse(P_time, out temptime)) { flag = false; updateDate = temptime; } } if (P_time.ToLower() == "y") { flag = false; } } #endregion Console.WriteLine("启动完毕, 更换文件时间节点是 " + updateDate.ToLongDateString()); writeCurrentRunDay();//把updateDate设置为今天 LogHelper.CreateErrorLogTxt(string.Format("-----------------------{0}-开始发布----------------------", DateTime.Now.ToLongDateString())); AllFiles allFiles = new AllFiles(); List <String> list = allFiles.FindFile(FolderPath, updateDate); //所有文件的物理地址 List <String> list2 = new List <string>(); //文件上传到云盘上的key集合 #region 文件上传到云盘上的key集合 foreach (var item in list) { //Console.WriteLine(item); string tempItem = item; tempItem = item.Replace(FolderPath, "").Replace('\\', '/'); tempItem = Pre_key + tempItem; list2.Add(tempItem); } #endregion LogHelper.CreateErrorLogTxt(string.Format("读取完毕所有的文件, 共计 {0}", list.Count)); var isError = false; for (int i = 0; i < list2.Count; i++) { string key = list2[i]; #region 到云 using (FileStream fs = new FileStream(list[i], FileMode.Open)) { //获得后缀名 var temp = list[i].Split('.'); string ext = temp.Length > 1 ? temp[temp.Length - 1] : ""; try { //if (i > 28) // throw new Exception(); //---------------上传到bos ObjectMetadata metadata = new ObjectMetadata(); metadata.ContentLength = fs.Length; metadata.ContentType = FileContentType.GetMimeType(ext); bos.PutObject(bos.OssConfig.BucketName, key, fs, metadata); Console.WriteLine("完成上传{0}----{1}", i, key); File.Delete(list[i]);//成功之后就删除 } catch (Exception ex) { isError = true; Console.WriteLine("处理{0}遇到问题, 代码是{1}, 当前进度{2}/{3}, 继续请按y", key, ex.Message, i + 1, list2.Count); var ifcontinue = Console.ReadLine(); if (ifcontinue.ToLower().Trim() != "y") { break; } LogHelper.CreateErrorLogTxt(string.Format("失败 {0}, {1}", key, ex.Message)); } } #endregion } LogHelper.CreateErrorLogTxt(string.Format("-----------------------{0}-完成----------------------\n\n\n", DateTime.Now.ToLongDateString())); if (!isError) { Console.WriteLine("任务完成, 没有出现任何问题,"); } else { Console.WriteLine("上传出现问题, 请查看日志"); deletecurrentday();//重置上次成功的上传时间 } return(updateDate); }