CORS 配置

  • 创建 CORS 配置并对存储桶设置该配置
  • 通过添加规则来检索并修改配置
  • 向存储桶添加修改过的配置
  • 删除配置
  1. import com.amazonaws.AmazonServiceException;
  2. import com.amazonaws.SdkClientException;
  3. import com.amazonaws.auth.profile.ProfileCredentialsProvider;
  4. import com.amazonaws.regions.Regions;
  5. import com.amazonaws.services.s3.AmazonS3;
  6. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  7. import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration;
  8. import com.amazonaws.services.s3.model.CORSRule;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. public class CORS {
  14. public static void main(String[] args) throws IOException {
  15. Regions clientRegion = Regions.DEFAULT_REGION;
  16. String bucketName = "*** Bucket name ***";
  17. // Create two CORS rules.
  18. List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();
  19. rule1AM.add(CORSRule.AllowedMethods.PUT);
  20. rule1AM.add(CORSRule.AllowedMethods.POST);
  21. rule1AM.add(CORSRule.AllowedMethods.DELETE);
  22. CORSRule rule1 = new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM)
  23. .withAllowedOrigins(Arrays.asList("http://*.example.com"));
  24. List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();
  25. rule2AM.add(CORSRule.AllowedMethods.GET);
  26. CORSRule rule2 = new CORSRule().withId("CORSRule2").withAllowedMethods(rule2AM)
  27. .withAllowedOrigins(Arrays.asList("*")).withMaxAgeSeconds(3000)
  28. .withExposedHeaders(Arrays.asList("x-amz-server-side-encryption"));
  29. List<CORSRule> rules = new ArrayList<CORSRule>();
  30. rules.add(rule1);
  31. rules.add(rule2);
  32. // Add the rules to a new CORS configuration.
  33. BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
  34. configuration.setRules(rules);
  35. try {
  36. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  37. .withCredentials(new ProfileCredentialsProvider())
  38. .withRegion(clientRegion)
  39. .build();
  40. // Add the configuration to the bucket.
  41. s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
  42. // Retrieve and display the configuration.
  43. configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
  44. printCORSConfiguration(configuration);
  45. // Add another new rule.
  46. List<CORSRule.AllowedMethods> rule3AM = new ArrayList<CORSRule.AllowedMethods>();
  47. rule3AM.add(CORSRule.AllowedMethods.HEAD);
  48. CORSRule rule3 = new CORSRule().withId("CORSRule3").withAllowedMethods(rule3AM)
  49. .withAllowedOrigins(Arrays.asList("http://www.example.com"));
  50. rules = configuration.getRules();
  51. rules.add(rule3);
  52. configuration.setRules(rules);
  53. s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
  54. // Verify that the new rule was added by checking the number of rules in the configuration.
  55. configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
  56. System.out.println("Expected # of rules = 3, found " + configuration.getRules().size());
  57. // Delete the configuration.
  58. s3Client.deleteBucketCrossOriginConfiguration(bucketName);
  59. System.out.println("Removed CORS configuration.");
  60. // Retrieve and display the configuration to verify that it was
  61. // successfully deleted.
  62. configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
  63. printCORSConfiguration(configuration);
  64. } catch (AmazonServiceException e) {
  65. // The call was transmitted successfully, but Amazon S3 couldn't process
  66. // it, so it returned an error response.
  67. e.printStackTrace();
  68. } catch (SdkClientException e) {
  69. // Amazon S3 couldn't be contacted for a response, or the client
  70. // couldn't parse the response from Amazon S3.
  71. e.printStackTrace();
  72. }
  73. }
  74. private static void printCORSConfiguration(BucketCrossOriginConfiguration configuration) {
  75. if (configuration == null) {
  76. System.out.println("Configuration is null.");
  77. } else {
  78. System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");
  79. for (CORSRule rule : configuration.getRules()) {
  80. System.out.println("Rule ID: " + rule.getId());
  81. System.out.println("MaxAgeSeconds: " + rule.getMaxAgeSeconds());
  82. System.out.println("AllowedMethod: " + rule.getAllowedMethods());
  83. System.out.println("AllowedOrigins: " + rule.getAllowedOrigins());
  84. System.out.println("AllowedHeaders: " + rule.getAllowedHeaders());
  85. System.out.println("ExposeHeader: " + rule.getExposedHeaders());
  86. System.out.println();
  87. }
  88. }
  89. }
  90. }

AmazonS3 使用AWS SDK for Java实现跨源资源共享 (CORS)的更多相关文章

  1. 跨源资源共享(CORS)概念、实现(用Spring)、起源介绍

    本文内容引用自: https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/ https://developer.m ...

  2. CORS跨源资源共享概念及配置(Kubernetes Ingress和Spring Cloud Gateway)

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 跨源资源共享CORS 跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种基于HTTP 头的机制,该机制通过 ...

  3. JS高程3:Ajax与Comet-进度事件、跨源资源共享

    有以下 6 个进度事件  loadstart:在接收到响应数据的第一个字节时触发.  progress:在接收响应期间持续不断地触发.  error:在请求发生错误时触发.  abort:在因 ...

  4. SpringBoot系列——CORS(跨源资源共享)

    前言 出于安全原因,浏览器禁止ajax调用当前源之外的资源(同源策略),我们之前也有写个几种跨域的简单实现(还在问跨域?本文记录js跨域的多种实现实例),本文主要详细介绍CORS,跨源资源共享,以及如 ...

  5. 彻底掌握CORS跨源资源共享

    本文来自于公众号链接: 彻底掌握CORS跨源资源共享 ) 本文接上篇公众号文章:彻底理解浏览器同源策略SOP 一.概述 在云时代,各种SAAS应用层出不穷,各种互联网API接口越来越丰富,H5技术在微 ...

  6. 跨域资源共享(CORS)问题解决方案

    CORS:Cross-Origin Resource Sharing(跨域资源共享) CORS被浏览器支持的版本情况如下:Chrome 3+.IE 8+.Firefox 3.5+.Opera 12+. ...

  7. 跨域资源共享CORS与JSONP

    同源策略限制: 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果没有同源策略,攻击者可以通过JavaScript获取你的邮件以及其他敏感信息,比如说 ...

  8. JavaScript跨源资源共享

    CORS(跨 源资源共享)基本思想,就是使用自定义的HTTP头部让浏览器与服务器进行沟通,从而决定请求或响应式应该成功还是失败 IE对CORS的实现 IE8引入了XDR类型,与XHR类似,但可以实现安 ...

  9. 跨域解决方案 - 跨域资源共享cors

    目录 1. cors 介绍 2. 原理 3. cors 解决跨域 4. 自定义HTTP 头部字段解决跨域 5. 代码演示 5. 参考链接 1. cors 介绍 cors 说的是一个机制,其实相当于一个 ...

随机推荐

  1. docker 支持systemctl start|stop|status等操作

    用docker运行centos7容器时候,无法使用systemctl,官方解释是centos7的一个bug,可以有修复的办法: 在docker run的时候,加上--privileged 并且cmd使 ...

  2. 3.安装可视化工具kibana

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-06-19 10:10:42 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  3. python = 赋值顺序 && C++ side effect

    title: python = 赋值顺序 && C++ side effect date: 2020-03-17 15:00:00 categories: [python][c++] ...

  4. pthread_create函数

    函数简介 pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t *re ...

  5. HDU 4272 LianLianKan(状压DP)题解

    题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ...

  6. vue2 响应式细节

    data 中的数据是如何处理的? 每一次实例化一个组件,都会调用 initData 然后调用 observe 方法,observe 方法调用了 new Observer(value), 并且返回 __ ...

  7. npm config set registry

    npm config set registry $ npm config set registry $ npm config set registry https://registry.your-re ...

  8. “NGK公链+5G”——打造智慧城市

    智慧城市目前被全球各国当成城市建设的重点,旨在城市在智能化的同时,还能给民众带来幸福感和安全感.随着5G的到来,城市智能化又到了一个新的高度.比如无人驾驶.无人机等方面将会产生质的变化,因为5G的加入 ...

  9. 一文助你了解NGK商城

    按照NGK的发展逻辑,将会在2021年上半年上线链商商城,解决传统消费行业真伪难辨的弊端,之后,将会推出小额支付功能,让NGK真正成为结算中的数字资产,目前两者落地性应用已经在开发内测阶段中,期初的部 ...

  10. 解决springBoot上传大文件异常问题

    上传文件过大时的报错: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size ex ...