阿里雲OSS C#回調服務實例代碼

先貼出客戶端上傳文件代碼和毀掉函數的定義web

須要的引用有:using Aliyun.OSS;json

經過nuget包,獲取aliyun.oss   dllapi

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回調服務器的實例代碼服務器

如下代碼暫時不包含簽名等參數;後面會追加加密參數等【等待嘗試後,後期更新代碼】;app

項目是webapi 寫的接口;ide

 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 }
OSS服務端 回到函數Code
相關文章
相關標籤/搜索