Exemplo n.º 1
0
        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);
        }