先贴出客户端上传文件代码和毁掉函数的定义
需要的引用有:using Aliyun.OSS;
通过nuget包,获取aliyun.oss dll
string url = "http://184.55.52.41:58123/api/Values/Put"; string postData = "{\"bucket\":\"up01\",\"fileName\":\"2.jpg\",\"etag\":\"image\",\"size\":\"1024\",\"mimeType\":\"5\",\"my_var1\":\"6\",\"my_var2\":\"7\"}"; private void btnUpOss_Click(object sender, EventArgs e) { //SampleUpload(); //return; OssClient client = new OssClient("oss-cn-beijing.aliyuncs.com", accessKeyId, accessKeySecret); postData = "bucket=${bucket}&fileName=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&my_var1=${x:var1}&my_var2=${x:var2}"; var metadata = BuildCallbackMetadata(url, postData); using (var fs = File.Open(textBox1.Text, FileMode.Open)) { var putObjectRequest = new PutObjectRequest(bucketName, "huTest.jpg", fs, metadata); var result = client.PutObject(putObjectRequest); string responseContent = GetCallbackResponse(result); textBox1.Text = responseContent; } } private ObjectMetadata BuildCallbackMetadata(string callbackUrl, string callbackBody) { string callbackHeaderBuilder = new CallbackHeaderBuilder(callbackUrl, callbackBody).Build(); string CallbackVariableHeaderBuilder = new CallbackVariableHeaderBuilder(). AddCallbackVariable("x:var1", "bidoStar").AddCallbackVariable("x:var2", "2017-09-19 15:35:12").Build(); var metadata = new ObjectMetadata(); metadata.AddHeader(HttpHeaders.Callback, callbackHeaderBuilder); metadata.AddHeader(HttpHeaders.CallbackVar, CallbackVariableHeaderBuilder); return metadata; } private string GetCallbackResponse(PutObjectResult putObjectResult) { string callbackResponse = null; using (var stream = putObjectResult.ResponseStream) { var buffer = new byte[4 * 1024]; var bytesRead = stream.Read(buffer, 0, buffer.Length); callbackResponse = Encoding.Default.GetString(buffer, 0, bytesRead); } return callbackResponse; }
oss回调服务器的实例代码
以下代码暂时不包含签名等参数;后面会追加加密参数等【等待尝试后,后期更新代码】;
项目是webapi 写的接口;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Net.Http; 6 using System.Web.Http; 7 using WebApiOss.Common; 8 using Newtonsoft.Json; 9 using System.Text; 10 11 namespace WebApiOss.Controllers 12 { 13 public class OSSController : ApiController 14 { 15 public string Get(string id,string name="pp") 16 { 17 Log4Helper.Info(name+"日志内容:" +id); 18 return "id="+id; 19 } 20 21 public JsonModel Post([FromBody]CallBackModel body) 22 { 23 Log4Helper.Info("------FileCallBack------"); 24 Log4Helper.Info(body.bucket + " " + body.fileName + " " + body.etag + " " + body.size + " " + body.mimeType + " " + body.my_var1 + " " + body.my_var2 + "&var1:" + body.var1 + " var2:" + body.var2); 25 return new JsonModel() { status = 200,msg=""}; 26 //return new HttpResponseMessage() 27 //{ 28 // StatusCode = HttpStatusCode.OK, 29 // Content = new StringContent("{'Status':'OK'}", 30 // Encoding.UTF8, "application/json") 31 //}; 32 } 33 34 35 36 public class JsonModel 37 { 38 public int status { get; set; } 39 public string msg { get; set; } 40 } 41 42 public class CallBackModel 43 { 44 public string bucket { get; set; } 45 public string fileName { get; set; } 46 public string etag { get; set; } 47 public string size { get; set; } 48 public string mimeType { get; set; } 49 public string my_var1 { get; set; } 50 public string my_var2 { get; set; } 51 public string var1 { get; set; } 52 public string var2 { get; set; } 53 } 54 } 55 }