using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GrapeCity.Forguncy.ServerApi; using OBS; using OBS.Model; namespace FileToHuaweiOBS { public class FileToHuaweiOBS :ForguncyApi { private static string endpoint = ""; private static string AK = ""; private static string SK = ""; private string LocalPath = ""; //本地附件目录 private string defaultObsregion = ""; //默认区域 private string defaultBucketName = ""; //默认桶名 private string obsregion = ""; //区域 private string BucketName = ""; //桶名 private string optype = ""; //操作类型 private string resultCode = ""; private string resultMsg = ""; //日志记录 Dictionary LogValues = new Dictionary(); [Post] public void obsApi() { //获取post请求的数据 var form = this.Context.Request.ReadFormAsync().Result; obsregion = form["obsregion"]; optype = form["optype"]; LogSave("api", "接收到POST", "操作:" + optype); optype = optype.ToLower(); //预留其他操作扩展 switch (optype) { case "fileasync": BucketName = form["BucketName"]; obsObjectAsync(obsregion, BucketName); break; default: this.resultCode = "1"; this.resultMsg = "操作方法错误"; break; } //定义返回结果; //转换数据类型为string if (resultCode == "0") { string result = Convert.ToString("0"); this.Context.Response.Write(result.ToString()); } else { string result = Convert.ToString("1") + "," + this.resultMsg; this.Context.Response.Write(result.ToString()); } } //OBS授权访问信息 public void obsAuth() { //获取默认区域 var data = DataAccess.GetTableData(string.Format("访问配置?$select=配置项值&$filter=配置项索引 eq 'obsregion'")) as List>; if (data.Count != 0) { //获取默认区域 defaultObsregion = data[0]["配置项值"].ToString(); //LogSave("OBS", "获取默认区域", defaultObsregion ); } //获取默认桶 data = DataAccess.GetTableData(string.Format("访问配置?$select=配置项值&$filter=配置项索引 eq 'BucketName'")) as List>; if (data.Count != 0) { //获取默认桶 defaultBucketName = data[0]["配置项值"].ToString(); //LogSave("OBS", "获取默认桶", defaultBucketName ); } if (obsregion == "") { obsregion = defaultObsregion;//设置默认区域 } //获取区域endpoint data = DataAccess.GetTableData(string.Format("区域终节点?$select=终节点&$filter=区域代码 eq '"+ obsregion + "'")) as List>; if (data.Count != 0) { //endpoint endpoint = data[0]["终节点"].ToString(); //LogSave("OBS", "endpoint", endpoint); } //获取AK data = DataAccess.GetTableData(string.Format("访问配置?$select=配置项值&$filter=配置项索引 eq 'OBSAK'")) as List>; if (data.Count != 0) { //AK AK = data[0]["配置项值"].ToString(); //LogSave("OBS", "AK", AK);//正式启用时注释此行,防止AK泄露 } //获取SK data = DataAccess.GetTableData(string.Format("访问配置?$select=配置项值&$filter=配置项索引 eq 'OBSSK'")) as List>; if (data.Count != 0) { //SK SK = data[0]["配置项值"].ToString(); //LogSave("OBS", "SK", SK);//正式启用时注释此行,防止SK泄露 } //获取本地附件目录 data = DataAccess.GetTableData(string.Format("访问配置?$select=配置项值&$filter=配置项索引 eq 'LOCALPATH'")) as List>; if (data.Count != 0) { //LocalPath LocalPath = data[0]["配置项值"].ToString(); //LogSave("OBS", "LocalPath", LocalPath); } } public void obsObjectAsync(string obsregion, string BucketName) { obsAuth(); //检查AK\SK\endpoint是否获取到 if (AK == "" || SK == "" || endpoint == "") { LogSave("OBS", "AK、SK、endpoint", "缺少配置"); return; } if (BucketName == "") { BucketName = defaultBucketName;//设置默认桶 } // 创建ObsClient实例 ObsClient client = new ObsClient(AK, SK, endpoint); int maxfileid = 0; string filepaths = ""; string appUpLoadPath = LocalPath;// "C:\\ProgramData\\Forguncy\\ibm\\3725\\WebSite\\Upload\"; LogSave("OBS桶对象Async", "maxfileid", "0"); var data = DataAccess.GetTableData(string.Format("文件表?$select=ID,附件&$filter=obs状态 eq 0&$top=1&$orderby=ID desc")) as List>; if (data.Count != 0) { maxfileid = Convert.ToInt32(data[0]["ID"].ToString()); filepaths = data[0]["附件"].ToString(); } LogSave("OBS桶对象Async", "filepaths成功", filepaths); string[] filepathArray = filepaths.Split('|'); // 上传文件 if (maxfileid >= 0) { try { foreach (string filename in filepathArray) { if (filename != "") { LogSave("OBS桶对象Async", "开始同步", filename); LogSave("OBS桶对象Async", "开始同步FilePath", appUpLoadPath + filename); LogSave("OBS桶对象Async", "开始同步到桶", BucketName); PutObjectRequest request = new PutObjectRequest() { BucketName = BucketName, ObjectKey = filename, FilePath = appUpLoadPath + filename,//待上传的本地文件路径,需要指定到具体的文件名 }; PutObjectResponse response = client.PutObject(request); LogSave("OBS桶对象", "文件同步成功", filename); } } //定义键为string,值为object类型的Dictionary Dictionary values = new Dictionary(); //定义要更新的记录 ColumnValuePair valuepair = new ColumnValuePair(); valuepair.ColumnName = "ID"; valuepair.Value = maxfileid; //要被更新的值,Key表示列名,值是要被更新的值 values = new Dictionary(); values.Add("obs状态", 1); //使用UpdateTableData方法更新OBS状态 this.DataAccess.UpdateTableData("文件表", valuepair, values); } catch (ObsException ex) { LogSave("OBS桶对象", "文件同步失败," + ex.ErrorCode, ex.ErrorMessage); } } } public void LogSave(string logType, string logTitle, string logBody) { Dictionary tempLogValues = new Dictionary(); tempLogValues.Clear(); tempLogValues.Add("日志类型", logType); tempLogValues.Add("标题", logTitle); tempLogValues.Add("内容", logBody); tempLogValues.Add("时间", DateTime.Now.ToString("hh:mm:ss")); tempLogValues.Add("日期", DateTime.Now.ToString("yyyy-MM-dd")); this.DataAccess.AddTableData("日志", tempLogValues); } } }
标签:string,obs,LogSave,API,ToString,BucketName,data,OBS From: https://www.cnblogs.com/outrun/p/17706381.html