阿里云OSS Web端直传 服务器签名C#版
最近用到队里OSS的文件上传,然后阿里官方给的四个服务器签名有Java PHP Python Go四个版本,就是没C#(话说写个C#有多难?) 百度了一下好像也没有,既然这样只能自己动手照着Java版本的改了.
下面是Java版的签名代码, 大概看一下就知道需要的是try代码块里的东西.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String endpoint = "*";
String accessId = "*";
String accessKey = "*";
String bucket = "*";
String dir = "user-dir";
String host = "http://" + bucket + "." + endpoint;
OSSClient client = new OSSClient(endpoint, accessId, accessKey);
try {
long e = 30L;
long expireEndTime = System.currentTimeMillis() + e * 1000L;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem("content-length-range", 0L, 1048576000L);
policyConds.addConditionItem(MatchMode.StartWith, "key", dir);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
LinkedHashMap respMap = new LinkedHashMap();
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000L));
JSONObject ja1 = JSONObject.fromObject(respMap);
System.out.println(ja1.toString());
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, POST");
this.response(request, response, ja1.toString());
} catch (Exception var22) {
Assert.fail(var22.getMessage());
}
}
下面是修改为C#版的代码,如果报403错误记得设置Cors:
public static string _id { get; } = "**********"; //AccessKeyId
public static string _key { get; } = "**********************************"; //AccessKeySecret
public static string _host { get; } = "http://*****.oss-cn-shenzhen.aliyuncs.com";
// GET: Test
[HttpGet]
public JsonResult Index()
{
OssClient client = new OssClient(_host, _id, _key);
DateTime now = DateTime.Now;
DateTime ex = now.AddSeconds(30);
PolicyConditions policyConds = new PolicyConditions();
policyConds.AddConditionItem("content-length-range", 0L, 1048576000L);
policyConds.AddConditionItem(MatchMode.StartWith, "key", "ic");
String postPolicy = client.GeneratePostPolicy(ex, policyConds);
byte[] binaryData = Encoding.Default.GetBytes(postPolicy);
String encodedPolicy = Convert.ToBase64String(binaryData);
//改到这里的时候遇到的一个坑 Java 里用的是SDK里: client.calculatePostSignature 方法生成签名,但是.NET-SDK好像没有用来生成签名的方法(难道是我找的不够仔细?).没办法又下载了PHP版本找到签名那段:
//$signature = base64_encode(hash_hmac(‘sha1‘, $string_to_sign, $key, true));
//既然知道了签名是用SHA1也就不需要SDK了.
var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(_key));
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(encodedPolicy));
var Signature = Convert.ToBase64String(hashBytes);
return Json(new
{
accessid = _id,
policy = encodedPolicy,
signature = Signature,
dir = "ic",
host = _host,
expire = ConvertDateTimeInt(ex).ToString()
}, JsonRequestBehavior.AllowGet);
}
文章来自:http://www.cnblogs.com/coolyuwk/p/5575696.html