private static void TempFunction(AliyunOpenStorageService aos) { Aliyun.OpenServices.OpenStorageService.ObjectListing result = null; string nextMarker = string.Empty; try { //do //{ #region 往list里面写入这个站点所有的文件key var listObjectsRequest = new Aliyun.OpenServices.OpenStorageService.ListObjectsRequest(aos.OssConfig.BucketName) { Marker = nextMarker, MaxKeys = 100 , Prefix = "" }; result = aos.ListObjects(listObjectsRequest); 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; } } nextMarker = result.NextMarker; #endregion Console.WriteLine("读取到{0}个文件", result.ObjectSummaries.Count()); //} while (result.IsTruncated);//(false);// } catch (Exception ex) { Console.WriteLine("error {0}", ex.Message); } }
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 void TemplateTransfer_forAliyun(AliyunOpenStorageService aos, AliyunOpenStorageService aosTarget) { var rootpath = Directory.GetCurrentDirectory(); var sourceFile = rootpath + "//app_data//Transfer_Ali_SourceSiteIDs.txt"; var FinishedIdsFile = rootpath + "//app_data//Transfer_Ali_FinishedIDs.txt"; var ErrorFile = rootpath + "//app_data//Transfer_Ali_Errors.txt"; Console.WriteLine("\n\n欢迎使用阿里云站点文件迁移工具."); if (!File.Exists(sourceFile)) { Console.WriteLine("未能找到Transfer_Ali_SourceSiteIDs.txt(源头云需要迁移的siteid集合文件)"); return; } //本次需要上传的文件里面的id集合 string[] sourceids = File.ReadAllText(sourceFile).Split(','); for (int i = 0; i < sourceids.Length; i++) { string tempsorceid = sourceids[i].Replace("\r", "").Replace("\n", ""); sourceids[i] = tempsorceid; } Console.WriteLine("\n文件中共有{0}个站点, 系统将会从日志中匹配未迁移过的站点.", sourceids.Length); var list = new List <string>(); //获取云端所有文件, 并在下载的时候下载成功一个移除一个项目 var uplistkey = new List <string>(); //list副本, 上传的时候读取 var uplistFullName = new List <string>(); for (int i = 0; i < sourceids.Length; i++) { //json日志文件 var FinishedList = JsonConvert.DeserializeAnonymousType(File.ReadAllText(FinishedIdsFile), new[] { new { SiteID = 0, FinishedDatetime = "" } }).ToList(); bool thisSiteHasError = false; //该站点在迁移过程全程是否发生过问题 string ErrorMessage = ""; //如果有则用这个记录 list.Clear(); uplistkey.Clear(); //清掉 int tempsiteid = 0, targetSiteID = 0; //香港迁移美国的规则id一致 if (!int.TryParse(sourceids[i], out tempsiteid)) { continue; } targetSiteID = tempsiteid; var tempobj = FinishedList.Where(x => x.SiteID == tempsiteid).FirstOrDefault(); if (tempobj != null) { continue; } #region 开始读取SiteID: {0} ,往list里面写入这个站点所有的文件 var index = 1; Aliyun.OpenServices.OpenStorageService.ObjectListing result = null; string nextMarker = string.Empty; Console.WriteLine("开始读取SiteID: {0}", tempsiteid); try { do { #region 往list里面写入这个站点所有的文件key var listObjectsRequest = new Aliyun.OpenServices.OpenStorageService.ListObjectsRequest(aos.OssConfig.BucketName) { Marker = nextMarker, MaxKeys = 100 , Prefix = "content/sitefiles/" + tempsiteid.ToString() + "/"//只有图片的话不需要sitefiles分组 }; result = aos.ListObjects(listObjectsRequest); 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; #endregion } while (result.IsTruncated);//(false);// } catch (Exception ex) { thisSiteHasError = true; ErrorMessage += "读取云端文件key到集合list的时候出现错误" + ex.Message; } #region 读取的过程中发生错误的话, 就判断这个站点读取失败, 记录到日志里面, continue循环, 读取下一个站点 if (thisSiteHasError) { var ErrorList = JsonConvert.DeserializeAnonymousType(File.ReadAllText(ErrorFile), new[] { new { SiteID = 0, FinishedDatetime = "", Message = "" } }).ToList(); ErrorList.Add(new { SiteID = tempsiteid, FinishedDatetime = DateTime.Now.ToLongTimeString(), Message = ErrorMessage }); string strtemperror = JsonConvert.SerializeObject(ErrorList); File.WriteAllText(ErrorFile, strtemperror); continue; } #endregion Console.WriteLine("\n\n Siteid:{1}, 共{0}个文件, 现在开始下载...", list.Count, tempsiteid); uplistkey = list.ToArray().ToList(); #endregion #region 载到本地 目标是程序所在文件夹 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)); list.Remove(curr); } } catch (Exception ex) { Console.WriteLine("siteid:{1}, 出现错误, 信息是{0}", ex.Message, tempsiteid); Console.WriteLine("重试中......"); } } Console.WriteLine("下载完成"); #endregion #region 到目标云 Console.WriteLine("开始上传"); while (uplistkey.Count > 0) { bool tempuploaderror = false; //记录上传的过程中是否有错误 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 { Aliyun.OpenServices.OpenStorageService.ObjectMetadata metadata = new Aliyun.OpenServices.OpenStorageService.ObjectMetadata(); metadata.ContentLength = fs.Length; metadata.ContentType = FileContentType.GetMimeType(ext); aosTarget.PutObject(aosTarget.OssConfig.BucketName, targetKey, fs, metadata); uplistkey.Remove(sourceKey); index++; } catch (Exception ex) { tempuploaderror = true; Console.WriteLine("上传中出错,siteid:{0}, message:{1}", tempsiteid, ex.Message); System.Threading.Thread.Sleep(2000); } } if (!tempuploaderror)//上传成功, 删除本地文件 { File.Delete(sourceKey); } } Console.WriteLine("siteid:{0}上传完成", tempsiteid); #endregion //该站点完成, 写入日志 FinishedList.Add(new { SiteID = tempsiteid, FinishedDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }); Console.WriteLine("siteid:{0}写入日志\n\n", tempsiteid); string strtemp = JsonConvert.SerializeObject(FinishedList); File.WriteAllText(FinishedIdsFile, strtemp); } Console.WriteLine("\n\n\n\n任务全部完成, 请查看日志"); Console.ReadLine(); }
static void Main(string[] args) { //配置文件 DateTime updateDate = Convert.ToDateTime(getCurrentDay()); //Convert.ToDateTime(System.Configuration.ConfigurationSettings.AppSettings[0]); string FolderPath = System.Configuration.ConfigurationSettings.AppSettings[0]; string Pre_key = System.Configuration.ConfigurationSettings.AppSettings[1]; //云盘上的key根目录 OssConfig config = OssConfigMgr.GetOneConfig(); BosOpenStorageService bos = new BosOpenStorageService(config); OssConfig aliConfig = OssConfigMgr.GetConfig(2); AliyunOpenStorageService aos = new AliyunOpenStorageService(aliConfig); OssConfig ali_hk_Config = OssConfigMgr.GetConfig(3); AliyunOpenStorageService hk_aos = new AliyunOpenStorageService(ali_hk_Config); OssConfig ali_us_Config = OssConfigMgr.GetConfig(4); AliyunOpenStorageService us_aos = new AliyunOpenStorageService(ali_us_Config); try { var flag1 = true; var which = ""; while (flag1) { Console.WriteLine("\n\n\n欢迎使用云梦云端帮助工具"); Console.WriteLine("请输入数字选择程序: \n1上传(支持百度) \n2删除(支持百度) \n3模版文件迁移(从阿里云到百度云) \n4整站文件多平台迁移(待完成) \n5模板文件迁移(从阿里香港云到百度) \n6 阿里云文件迁移(香港云到美国云)"); which = Console.ReadLine(); if (which != "1" && which != "2" && which != "3" && which != "5" && which != "6" && which != "temp") { continue; } Console.WriteLine(""); Console.WriteLine(""); switch (which) { case "1": updateDate = RunUpload(updateDate, FolderPath, Pre_key, bos); break; case "2": delete(bos); break; case "3": TemplateTransfer(bos, aos); break; case "5": TemplateTransfer(bos, hk_aos); break; case "6": TemplateTransfer_forAliyun(hk_aos, us_aos); break; case "temp": //TempFunction(us_aos); VisitUSA(); break; } } } catch (Exception ex) { LogHelper.CreateErrorLogTxt(ex.Message); } Console.Read(); }