1、引用Aliyun.OSS.dll
2、
static string _aliyun_oss_region = ConfigurationManager.AppSettings["aliyun_oss_region"]; static string _aliyun_oss_accessKeyId = ConfigurationManager.AppSettings["aliyun_oss_accessKeyId"]; static string _aliyun_oss_accessKeySecret = ConfigurationManager.AppSettings["aliyun_oss_accessKeySecret"]; static string _aliyun_oss_bucket = ConfigurationManager.AppSettings["aliyun_oss_bucket"]; public static dynamic GeneratePostPolicy() { string dir = "user_note/"; string host = "http://" + _aliyun_oss_bucket + "." + _aliyun_oss_region; OssClient client = new OssClient(host, _aliyun_oss_accessKeyId, _aliyun_oss_accessKeySecret); //过期时间 DateTime expiration = DateTime.Now.AddHours(24); PolicyConditions policyConds = new PolicyConditions(); //请求长度限制1024X1024X1000 policyConds.AddConditionItem(PolicyConditions.CondContentLengthRange, 0, 1048576000); policyConds.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, dir); string postPolicy = client.GeneratePostPolicy(expiration, policyConds); byte[] binaryData = System.Text.Encoding.UTF8.GetBytes(postPolicy); string encodedPolicy = Convert.ToBase64String(binaryData); var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(_aliyun_oss_accessKeySecret)); byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(encodedPolicy)); string postSignature = Convert.ToBase64String(hashBytes); return new { accessid = _aliyun_oss_accessKeyId, policy = encodedPolicy, signature = postSignature, dir = dir, host = host, expire = (expiration.ToUniversalTime().Ticks - 621355968000000000) / 10000 }; }
那些年我们踩过的坑