public static void SetBucketCors(string bucketName) { try { var request = new SetBucketCorsRequest(bucketName); var rule1 = new CORSRule(); // Note: AllowedOrigin & AllowdMethod must not be empty. rule1.AddAllowedOrigin("http://www.a.com"); rule1.AddAllowedMethod("POST"); rule1.AddAllowedHeader("*"); rule1.AddExposeHeader("x-oss-test"); request.AddCORSRule(rule1); var rule2 = new CORSRule(); rule2.AddAllowedOrigin("http://www.b.com"); rule2.AddAllowedMethod("GET"); rule2.AddExposeHeader("x-oss-test2"); rule2.MaxAgeSeconds = 100; request.AddCORSRule(rule2); client.SetBucketCors(request); Console.WriteLine("Set bucket:{0} Cors succeeded ", bucketName); } catch (OssException ex) { Console.WriteLine("Failed with error info: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static void SetBucketCors() { const string accessKeyId = "<your access key id>"; const string accessKeySecret = "<your access key secret>"; const string endpoint = "<valid host name>"; const string bucketName = "<bucket name>"; OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret); try { var req = new SetBucketCorsRequest(bucketName); var r1 = new CORSRule(); // Note: AllowedOrigin & AllowdMethod must not be empty. r1.AddAllowedOrigin("http://www.a.com"); r1.AddAllowedMethod("POST"); r1.AddAllowedHeader("*"); r1.AddExposeHeader("x-oss-test"); req.AddCORSRule(r1); var r2 = new CORSRule(); r2.AddAllowedOrigin("http://www.b.com"); r2.AddAllowedMethod("GET"); r2.AddExposeHeader("x-oss-test2"); r2.MaxAgeSeconds = 1000000000; req.AddCORSRule(r2); client.SetBucketCors(req); var rs = client.GetBucketCors(bucketName); foreach (var r in rs) { // do something with CORSRule here... } } catch (OssException ex) { Console.WriteLine("Failed with error info: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } finally { client.DeleteBucketCors(bucketName); } }
public void EnableBucketCorsAddAndDeleteSingleRuleTest() { try { var sbcRequest = new SetBucketCorsRequest(_bucketName); sbcRequest.AddCORSRule(ConstructDummyCorsRule()); _ossClient.SetBucketCors(sbcRequest); OssTestUtils.WaitForCacheExpire(); var rules = OssTestUtils.ToArray <CORSRule>(_ossClient.GetBucketCors(_bucketName)); Assert.AreEqual(1, rules.Count); } finally { _ossClient.DeleteBucketCors(_bucketName); OssTestUtils.WaitForCacheExpire(); try { _ossClient.GetBucketCors(_bucketName); Assert.Fail("Get bucket cors should not pass when it was not set or deleted"); } catch (OssException e) { Assert.AreEqual(OssErrorCode.NoSuchCORSConfiguration, e.ErrorCode); } } }
public Task <bool> SetBucketCorsRequestAsync(string bucketName, List <BucketCorsRule> rules) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (rules == null || rules.Count == 0) { throw new ArgumentNullException(nameof(rules)); } var request = new SetBucketCorsRequest(bucketName); foreach (var item in rules) { var rule = new CORSRule(); // 指定允许跨域请求的来源。 rule.AddAllowedOrigin(item.Origin); // 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD)。 rule.AddAllowedMethod(item.Method.ToString()); // AllowedHeaders和ExposeHeaders不支持通配符。 rule.AddAllowedHeader(item.AllowedHeader); // 指定允许用户从应用程序中访问的响应头。 rule.AddExposeHeader(item.ExposeHeader); request.AddCORSRule(rule); } // 设置跨域资源共享规则。 _client.SetBucketCors(request); return(Task.FromResult(true)); }
public void EnableBucketCorsAddAndDeleteMultipleRulesTest() { try { var sbcRequest = new SetBucketCorsRequest(_bucketName); sbcRequest.AddCORSRule(ConstructDummyCorsRule()); sbcRequest.AddCORSRule(ConstructDummyCorsRuleWithMultiAllowedMethod()); _ossClient.SetBucketCors(sbcRequest); OssTestUtils.WaitForCacheExpire(); var rules = OssTestUtils.ToArray <CORSRule>(_ossClient.GetBucketCors(_bucketName)); Assert.AreEqual(2, rules.Count); } finally { _ossClient.DeleteBucketCors(_bucketName); } }
public void EnableBucketCorsEmptyTest() { var sbcRequest = new SetBucketCorsRequest(_bucketName); var newRole = new CORSRule(); try { sbcRequest.AddCORSRule(newRole); Assert.Fail("Add corsrule without any settings should not pass"); } catch (ArgumentException) { Assert.IsTrue(true); } }
public void SetBucketCors(string bucketName, string allowedOrigin, string allowedMethod, string allowedHeader, string exposeHeader) { var req = new SetBucketCorsRequest(bucketName); var r1 = new CORSRule(); //指定允许跨域请求的来源 r1.AddAllowedOrigin(allowedOrigin); //指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) r1.AddAllowedMethod(allowedMethod); //控制在OPTIONS预取指令中Access-Control-Request-Headers头中指定的header是否允许。 r1.AddAllowedHeader(allowedHeader); //指定允许用户从应用程序中访问的响应头 r1.AddExposeHeader(exposeHeader); req.AddCORSRule(r1); Client.SetBucketCors(req); }
public void EnableBucketCorsAddInvalidSingleRuleTest() { try { var sbcRequest = new SetBucketCorsRequest(_bucketName); var rule = new CORSRule(); rule.AddAllowedOrigin("Original " + Guid.NewGuid()); rule.AddAllowedMethod("GETGET"); sbcRequest.AddCORSRule(rule); _ossClient.SetBucketCors(sbcRequest); Assert.Fail("Invalid Cors Rule should not be created successfully"); } catch (ArgumentException) { Assert.IsTrue(true); } finally { _ossClient.DeleteBucketCors(_bucketName); } }
/// <summary> /// 跨域规则 /// </summary> public SetBucketCorsRequest ToCORSRules() { var request = new SetBucketCorsRequest(Name); var rule = new CORSRule(); rule.MaxAgeSeconds = MaxAgeSeconds; if (AllowedOrigins != null && AllowedOrigins.Length > 0) { AllowedOrigins.ToList().ForEach(x => rule.AddAllowedOrigin(x)); } else { throw new Abp.UI.UserFriendlyException("AllowedOrigins 不能是空"); } if (AllowedHeaders != null && AllowedHeaders.Length > 0) { AllowedHeaders.ToList().ForEach(x => rule.AddAllowedHeader(x)); } if (AllowedMethods != null && AllowedMethods.Length > 0) { AllowedMethods.ToList().ForEach(x => rule.AddAllowedMethod(x)); } else { throw new Abp.UI.UserFriendlyException("AllowedMethod 不能是空"); } if (ExposedHeaders != null && ExposedHeaders.Length > 0) { if (ExposedHeaders.Contains("*")) { throw new Abp.UI.UserFriendlyException("ExposedHeaders 不能使用*通配符"); } ExposedHeaders.ToList().ForEach(x => rule.AddExposeHeader(x)); } request.AddCORSRule(rule); return(request); }
public void SetBucketCorsRequestInvalidArgumentTest() { try { var sbcRequest = new SetBucketCorsRequest(_bucketName); sbcRequest.AddCORSRule(null); Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("corsRule should not be null or empty")); } try { var sbcRequest = new SetBucketCorsRequest(_bucketName); var rule = new CORSRule(); rule.AddAllowedOrigin("Original " + Guid.NewGuid()); rule.AddAllowedMethod("GET"); for (var i = 0; i < 12; i++) { sbcRequest.AddCORSRule(rule); } Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("One bucket not allow exceed ten item of CORSRules")); } try { var sbcRequest = new SetBucketCorsRequest(_bucketName); var rule = new CORSRule(); sbcRequest.AddCORSRule(rule); Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("corsRule.AllowedOrigins should not be empty")); } try { var sbcRequest = new SetBucketCorsRequest(_bucketName); var rule = new CORSRule(); rule.AddAllowedOrigin("Original " + Guid.NewGuid()); sbcRequest.AddCORSRule(rule); Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("corsRule.AllowedMethods should not be empty")); } try { var sbcRequest = new SetBucketCorsRequest(_bucketName); sbcRequest.CORSRules = null; Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("CORSRule list should not be null")); } try { var corsRules = new List <CORSRule>(); var rule = new CORSRule(); rule.AddAllowedOrigin("Original " + Guid.NewGuid()); rule.AddAllowedMethod("GET"); for (var i = 0; i < 9; i++) { corsRules.Add(rule); } var sbcRequest = new SetBucketCorsRequest(_bucketName); sbcRequest.CORSRules = corsRules; Assert.IsTrue(true); corsRules.Add(rule); corsRules.Add(rule); sbcRequest.CORSRules = corsRules; Assert.Fail("Invalid argument should not be successful"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains(" bucket not allow exceed ten item of CORSRules")); } }