全部產品
Search
文件中心

Object Storage Service:跨域資源共用

更新時間:Aug 30, 2018

跨域資源共用(Cross-origin resource sharing,簡稱CORS)允許Web端的應用程式訪問不屬於本域的資源。OSS提供跨域資源共用介面,方便您控制跨域訪問的許可權。

更多關於跨域資源共用的介紹,請參見開發指南中的設定跨域訪問和API參考中的PutBucketcors

設定跨域資源共用規則

以下代碼用於設定指定儲存空間的跨域資源共用規則:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // 建立OSSClient執行個體。
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. SetBucketCORSRequest request = new SetBucketCORSRequest(bucketName);
  10. // 跨域資源共用規則的容器,每個儲存空間最多允許10條規則。
  11. ArrayList<CORSRule> putCorsRules = new ArrayList<CORSRule>();
  12. CORSRule corRule = new CORSRule();
  13. ArrayList<String> allowedOrigin = new ArrayList<String>();
  14. // 指定允許跨域請求的來源。
  15. allowedOrigin.add( "http://www.b.com");
  16. ArrayList<String> allowedMethod = new ArrayList<String>();
  17. // 指定允許的跨域要求方法(GET/PUT/DELETE/POST/HEAD)。
  18. allowedMethod.add("GET");
  19. ArrayList<String> allowedHeader = new ArrayList<String>();
  20. // 是否允許預取指令(OPTIONS)中Access-Control-Request-Headers頭中指定的Header。
  21. allowedHeader.add("x-oss-test");
  22. ArrayList<String> exposedHeader = new ArrayList<String>();
  23. // 指定允許使用者從應用程式中訪問的回應標頭。
  24. exposedHeader.add("x-oss-test1");
  25. // AllowedOrigins和AllowedMethods最多支援一個星號(*)萬用字元。星號(*)表示允許所有的域來源或者操作。
  26. corRule.setAllowedMethods(allowedMethod);
  27. corRule.setAllowedOrigins(allowedOrigin);
  28. // AllowedHeaders和ExposeHeaders不支援萬用字元。
  29. corRule.setAllowedHeaders(allowedHeader);
  30. corRule.setExposeHeaders(exposedHeader);
  31. // 指定瀏覽器對特定資源的預取(OPTIONS)請求返回結果的緩存時間,單位為秒。
  32. corRule.setMaxAgeSeconds(10);
  33. // 最多允許10條規則。
  34. putCorsRules.add(corRule);
  35. // 已存在的規則將被覆蓋。
  36. request.setCorsRules(putCorsRules);
  37. ossClient.setBucketCORS(request);
  38. // 關閉OSSClient。
  39. ossClient.shutdown();

獲取跨域資源共用規則

以下代碼用於獲取跨域資源共用規則:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // 建立OSSClient執行個體。
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. ArrayList<CORSRule> corsRules;
  10. // 獲取跨域資源共用規則列表。
  11. corsRules = (ArrayList<CORSRule>) ossClient.getBucketCORSRules(bucketName);
  12. for (CORSRule rule : corsRules) {
  13. for (String allowedOrigin1 : rule.getAllowedOrigins()) {
  14. // 獲取允許的跨域請求源。
  15. System.out.println(allowedOrigin1);
  16. }
  17. for (String allowedMethod1 : rule.getAllowedMethods()) {
  18. // 獲取允許的跨域要求方法。
  19. System.out.println(allowedMethod1);
  20. }
  21. if (rule.getAllowedHeaders().size() > 0){
  22. for (String allowedHeader1 : rule.getAllowedHeaders()) {
  23. // 獲取允許的頭部列表。
  24. System.out.println(allowedHeader1);
  25. }
  26. }
  27. if (rule.getExposeHeaders().size() > 0) {
  28. for (String exposeHeader : rule.getExposeHeaders()) {
  29. // 獲取允許的頭部。
  30. System.out.println(exposeHeader);
  31. }
  32. }
  33. if ( null != rule.getMaxAgeSeconds()) {
  34. System.out.println(rule.getMaxAgeSeconds());
  35. }
  36. }
  37. // 關閉OSSClient。
  38. ossClient.shutdown();

刪除跨域資源共用規則

以下代碼用於刪除指定儲存空間的所有跨域資源共用規則:

  1. // Endpoint以杭州為例,其它Region請按實際情況填寫。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // 建立OSSClient執行個體。
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. // 刪除儲存空間的跨域資源共用規則。
  10. ossClient.deleteBucketCORSRules(bucketName);
  11. // 關閉OSSClient。
  12. ossClient.shutdown();