全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

管理存储空间CORS跨域规则(Java)

最近更新时间:2021-12-16 11:41:52

设置bucket的CORS跨域规则

public void putBucketCors(){
       // yourEndpoint填写Bucket所在地域对应的Endpoint。以中国(北京)为例,Endpoint填写为ks3-cn-beijing.ksyuncs.com。如果使用自定义域名,设置endpoint为自定义域名,同时设置domainMode为true
       String endpoint = "yourEndpoint";
       // 金山云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
       String accessKeyId = "yourAccessKeyId";
       String accessKeySecret = "yourAccessKeySecret";
       // 创建Ks3ClientConfig 实例。
       Ks3ClientConfig config = new Ks3ClientConfig();
       // 设置域名
       config.setEndpoint(endpoint);
       // 创建Ks3Client实例
       Ks3 client = new Ks3Client(accessKeyId, accessKeySecret, config);
       // 创建BucketCors配置
       BucketCorsConfiguration config = new BucketCorsConfiguration();
       // 资源跨域共享规则
       CorsRule rule1 = new CorsRule();
       // 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) 
       List<AllowedMethods> allowedMethods = new ArrayList<AllowedMethods>();
       allowedMethods.add(AllowedMethods.GET);
       // 指定允许跨域请求的来源 
       List<String> allowedOrigins = new ArrayList<String>();
       // 指定允许跨域请求的来源地址为:http://example.com
       allowedOrigins.add("http://example.com");
       // 指定允许用户从应用程序中访问的响应头 
       List<String> exposedHeaders = new ArrayList<String>();
       // 指定允许用户从应用程序中访问的响应头 x-kss-test1
       exposedHeaders.add("x-kss-test1");
       // 控制在 OPTIONS 预取指令中 Access-Control-Request-Headers 头中指定的 header 是否允许。
       List<String> allowedHeaders = new ArrayList<String>();
       // 指定允许的header为 x-kss-test
       allowedHeaders.add("x-kss-test");
       // 设置 OPTIONS 预取指令中 Access-Control-Request-Headers 头中允许的 header 
       rule1.setAllowedHeaders(allowedHeaders);
       // 设置允许的跨域请求方法
       rule1.setAllowedMethods(allowedMethods);
       // 设置允许的跨域请求的来源地址
       rule1.setAllowedOrigins(allowedOrigins);
       // 设置允许用户从应用程序中访问的响应头 
       rule1.setExposedHeaders(exposedHeaders);
       // 指定浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒。 
       rule1.setMaxAgeSeconds(200);
       // 添加规则
       config.addRule(rule1);
       // 一个bucket可以最多配置10条规则
       // 配置第二条跨域规则
       CorsRule rule2 = new CorsRule();
       List<AllowedMethods> allowedMethods2 = new ArrayList<AllowedMethods>();
       allowedMethods2.add(AllowedMethods.GET);
       allowedMethods2.add(AllowedMethods.POST);
       List<String> allowedOrigins2 = new ArrayList<String>();
       allowedOrigins2.add("http://example.com");
       allowedOrigins2.add("http://*.example.com");
       List<String> exposedHeaders2 = new ArrayList<String>();
       exposedHeaders2.add("x-kss-test1");
       exposedHeaders2.add("x-kss-test2");
       List<String> allowedHeaders2 = new ArrayList<String>();
       allowedHeaders2.add("x-kss-test"); 
       allowedHeaders2.add("x-kss-test2"); 
       rule2.setAllowedHeaders(allowedHeaders2);
       rule2.setAllowedMethods(allowedMethods2);
       rule2.setAllowedOrigins(allowedOrigins2);
       rule2.setExposedHeaders(exposedHeaders2);
       rule2.setMaxAgeSeconds(500);
       config.addRule(rule2);
       // 创建配置bucketCors请求
       PutBucketCorsRequest request = new PutBucketCorsRequest("<您的bucket名称>",config);
       client.putBucketCors(request);
}

注意:

  • CORS是指跨域资源共享,当使用javascript进行跨域的时候,需要为bucket配置该规则。W3C文档
  • AllowedMethods、AllowedHeaders和AllowedOrigins为必选参数。AllowedHeaders和AllowedOrigins中支持最多一个*号的通配。

获取bucket的CORS跨域规则

public BucketCorsConfiguration getBucketCors(){
       // yourEndpoint填写Bucket所在地域对应的Endpoint。以中国(北京)为例,Endpoint填写为ks3-cn-beijing.ksyuncs.com。如果使用自定义域名,设置endpoint为自定义域名,同时设置domainMode为true
       String endpoint = "yourEndpoint";
       // 金山云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
       String accessKeyId = "yourAccessKeyId";
       String accessKeySecret = "yourAccessKeySecret";
       // 创建Ks3ClientConfig 实例。
       Ks3ClientConfig config = new Ks3ClientConfig();
       // 设置域名
       config.setEndpoint(endpoint);
       // 创建Ks3Client实例
       Ks3 client = new Ks3Client(accessKeyId, accessKeySecret, config);
       // 获取bucketCors配置请求
       BucketCorsConfiguration config = client.getBucketCors("test.bucket");
       List<CorsRule> rules = config.getRules();
       for(CorsRule rule : rules){
              //控制在 OPTIONS 预取指令中 Access-Control-Request-Headers 头中指定的 header 是否允许。
              rule.getAllowedHeaders();
              //允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD) 
              rule.getAllowedMethods();
              //允许跨域请求的来源 
              rule.getAllowedOrigins();
              //允许用户从应用程序中访问的响应头 
              rule.getExposedHeaders();
              //浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒。 
              rule.getMaxAgeSeconds();
       }
       // 返回配置
       return config;
}

删除bucket的CORS跨域规则

public void deleteBucketCors(){
       // yourEndpoint填写Bucket所在地域对应的Endpoint。以中国(北京)为例,Endpoint填写为ks3-cn-beijing.ksyuncs.com。如果使用自定义域名,设置endpoint为自定义域名,同时设置domainMode为true
       String endpoint = "yourEndpoint";
       // 金山云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
       String accessKeyId = "yourAccessKeyId";
       String accessKeySecret = "yourAccessKeySecret";
       // 创建Ks3ClientConfig 实例。
       Ks3ClientConfig config = new Ks3ClientConfig();
       // 设置域名
       config.setEndpoint(endpoint);
       // 创建Ks3Client实例
       Ks3 client = new Ks3Client(accessKeyId, accessKeySecret, config);
       // 删除桶名为test的Cors
       client.deleteBucketCors("test");
}
文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈