一、创建一个HuaWeiOBS类,作为数据传递的对象。
1 public class HuaWeiOBS 2 { 3 public string AK { get; set; } 4 public string SK { get; set; } 5 /// <summary> 6 /// OBS桶终结点 7 /// </summary> 8 public string EndPoint { get; set; } 9 /// <summary> 10 /// 要上传的问题件路径 11 /// </summary> 12 public string FilePath { get; set; } 13 /// <summary> 14 /// OBS桶名称 15 /// </summary> 16 public string BucketName { get; set; } 17 /// <summary> 18 /// 文件上传到OBS后的名称 19 /// </summary> 20 public string ObjectName { get; set; } 21 /// <summary> 22 /// 要上传到OBS的流 23 /// </summary> 24 public Stream InputStream{ get; set; } 25 }
二、封装一个上传华为云OBS的工具类。
1 public class UploadFileOBS 2 { 3 /// <summary> 4 /// 上传文件到OBS文件服务器 5 /// </summary> 6 /// <param name="filePath">待上传文件的本地路径</param> 7 /// <param name="obsBucketName">华为云OBS桶的名称</param> 8 /// <param name="obsObjectName">要上传到的OBS对象的名称</param> 9 /// <returns></returns> 10 public static bool UploadFileToOBS(HuaWeiOBS obs) 11 { 12 try 13 { 14 if (string.IsNullOrEmpty(obs.AK)) 15 { 16 MyLog4NetInfo.Error(MethodBase.GetCurrentMethod().DeclaringType, 17 new Exception("obs.AK不能为空,请添加配置【HuaWeiOBS:AK】")); 18 return false; 19 } 20 if (string.IsNullOrEmpty(obs.SK)) 21 { 22 MyLog4NetInfo.Error(MethodBase.GetCurrentMethod().DeclaringType, 23 new Exception("obs.SK不能为空,请添加配置【HuaWeiOBS:SK】")); 24 return false; 25 } 26 if (string.IsNullOrEmpty(obs.EndPoint)) 27 { 28 MyLog4NetInfo.Error(MethodBase.GetCurrentMethod().DeclaringType, 29 new Exception("obs.EndPoint不能为空,请添加配置【HuaWeiOBS:EndPoint】")); 30 return false; 31 } 32 if (string.IsNullOrEmpty(obs.BucketName)) 33 { 34 MyLog4NetInfo.Error(MethodBase.GetCurrentMethod().DeclaringType, 35 new Exception("obs.BucketName不能为空,请添加配置【HuaWeiOBS:BucketName】")); 36 return false; 37 } 38 ObsClient obsClient = new ObsClient(obs.AK, obs.SK, obs.EndPoint); 39 PutObjectRequest request = new PutObjectRequest(); 40 PutObjectResponse response = new PutObjectResponse(); 41 if (!string.IsNullOrEmpty(obs.FilePath)) 42 { 43 request.BucketName = obs.BucketName; 44 request.FilePath = obs.FilePath; 45 request.ObjectKey = obs.ObjectName; 46 response = obsClient.PutObject(request); 47 } 48 else if (obs.InputStream != null) 49 { 50 obs.InputStream.Seek(0, SeekOrigin.Begin); 51 request.BucketName = obs.BucketName; 52 request.InputStream = obs.InputStream; 53 request.ObjectKey = obs.ObjectName; 54 response = obsClient.PutObject(request); 55 } 56 else 57 { 58 //上传文件为空59 } 60 return response.StatusCode == System.Net.HttpStatusCode.OK; 61 } 62 catch (Exception e) 63 { 64 //打印异常 65 return false; 66 } 67 } 68 69 public static HuaWeiOBS GetOBSSetting() 70 { 71 HuaWeiOBS obs = new HuaWeiOBS(); 72 obs.AK = System.Configuration.ConfigurationManager.AppSettings["HuaWeiOBS:AK"]; 73 obs.SK = System.Configuration.ConfigurationManager.AppSettings["HuaWeiOBS:SK"]; 74 obs.EndPoint = System.Configuration.ConfigurationManager.AppSettings["HuaWeiOBS:EndPoint"]; 75 obs.BucketName = System.Configuration.ConfigurationManager.AppSettings["HuaWeiOBS:BucketName"]; 76 return obs; 77 }
三、文件路径(URL)和 流上传
1、流上传:将获取到的流以 byte[] 的方式存储起来,在调用 UploadFileOBS.UploadFileToOBS(HuaWeiOBS obs)方法前,需将存储好的 byte[] 转换成MemoryStream流。也就是 obs.InputStream = new MemoryStream(byteArray);
1 var obs = UploadFileOBS.GetOBSSetting(); 2 obs.InputStream = new MemoryStream(byteArray); 3 var isSuccess = UploadFileOBS.UploadFileToOBS(obs);
然后调用上面已经封装好的 UploadFileToOBS 方法,将HuaWeiOBS对象传递进去即可。
1 /// <summary> 2 /// </summary> 3 /// <param name="url">图片连接地址</param> 4 /// <param name="keyId">业务数据的主键</param> 5 /// <returns></returns> 6 public byte[] GetImageStream(string url) 7 { 8 if (string.IsNullOrEmpty(url)) 9 { 10 return null; 11 } 12 var client = new RestClient(url); 13 var restRequest = new RestRequest(); 14 var restResponse = client.Execute(restRequest); 15 if (restResponse.StatusCode == System.Net.HttpStatusCode.OK) 16 { 17 using (MemoryStream stream = new MemoryStream(restResponse.RawBytes)) 18 { 19 stream.Seek(0, SeekOrigin.Begin); 20 return stream.ToArray(); 21 } 22 } 23 else 24 { 25 //打印异常26 return null; 27 } 28 29 }
2、文件路径(URL):获取到文件的绝对路径,然后将改路径赋值给华为云OBS对象的FilePath属性。
1 var obs = UploadFileOBS.GetOBSSetting(); 2 var filePath = @“文件路径”; 3 obs.FilePath = filePath; 4 var isSuccess = UploadFileOBS.UploadFileToOBS(obs);
标签:string,HuaWeiOBS,obs,public,华为,new,NET,OBS From: https://www.cnblogs.com/zhengqitong/p/17767493.html