跨域資源共用(Cross-origin resource sharing,簡稱CORS)允許Web端的應用程式訪問不屬於本域的資源。OSS提供跨域資源共用介面,方便您控制跨域訪問的許可權。
更多關於跨域資源共用的介紹,請參見開發指南中的設定跨域訪問和API參考中的PutBucketcors。
設定跨域資源共用規則
以下代碼用於設定指定儲存空間的跨域資源共用規則:
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 建立OSSClient執行個體。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
SetBucketCORSRequest request = new SetBucketCORSRequest(bucketName);
// 跨域資源共用規則的容器,每個儲存空間最多允許10條規則。
ArrayList<CORSRule> putCorsRules = new ArrayList<CORSRule>();
CORSRule corRule = new CORSRule();
ArrayList<String> allowedOrigin = new ArrayList<String>();
// 指定允許跨域請求的來源。
allowedOrigin.add( "http://www.b.com");
ArrayList<String> allowedMethod = new ArrayList<String>();
// 指定允許的跨域要求方法(GET/PUT/DELETE/POST/HEAD)。
allowedMethod.add("GET");
ArrayList<String> allowedHeader = new ArrayList<String>();
// 是否允許預取指令(OPTIONS)中Access-Control-Request-Headers頭中指定的Header。
allowedHeader.add("x-oss-test");
ArrayList<String> exposedHeader = new ArrayList<String>();
// 指定允許使用者從應用程式中訪問的回應標頭。
exposedHeader.add("x-oss-test1");
// AllowedOrigins和AllowedMethods最多支援一個星號(*)萬用字元。星號(*)表示允許所有的域來源或者操作。
corRule.setAllowedMethods(allowedMethod);
corRule.setAllowedOrigins(allowedOrigin);
// AllowedHeaders和ExposeHeaders不支援萬用字元。
corRule.setAllowedHeaders(allowedHeader);
corRule.setExposeHeaders(exposedHeader);
// 指定瀏覽器對特定資源的預取(OPTIONS)請求返回結果的緩存時間,單位為秒。
corRule.setMaxAgeSeconds(10);
// 最多允許10條規則。
putCorsRules.add(corRule);
// 已存在的規則將被覆蓋。
request.setCorsRules(putCorsRules);
ossClient.setBucketCORS(request);
// 關閉OSSClient。
ossClient.shutdown();
獲取跨域資源共用規則
以下代碼用於獲取跨域資源共用規則:
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 建立OSSClient執行個體。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
ArrayList<CORSRule> corsRules;
// 獲取跨域資源共用規則列表。
corsRules = (ArrayList<CORSRule>) ossClient.getBucketCORSRules(bucketName);
for (CORSRule rule : corsRules) {
for (String allowedOrigin1 : rule.getAllowedOrigins()) {
// 獲取允許的跨域請求源。
System.out.println(allowedOrigin1);
}
for (String allowedMethod1 : rule.getAllowedMethods()) {
// 獲取允許的跨域要求方法。
System.out.println(allowedMethod1);
}
if (rule.getAllowedHeaders().size() > 0){
for (String allowedHeader1 : rule.getAllowedHeaders()) {
// 獲取允許的頭部列表。
System.out.println(allowedHeader1);
}
}
if (rule.getExposeHeaders().size() > 0) {
for (String exposeHeader : rule.getExposeHeaders()) {
// 獲取允許的頭部。
System.out.println(exposeHeader);
}
}
if ( null != rule.getMaxAgeSeconds()) {
System.out.println(rule.getMaxAgeSeconds());
}
}
// 關閉OSSClient。
ossClient.shutdown();
刪除跨域資源共用規則
以下代碼用於刪除指定儲存空間的所有跨域資源共用規則:
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里雲主帳號AccessKey擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立RAM帳號。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// 建立OSSClient執行個體。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 刪除儲存空間的跨域資源共用規則。
ossClient.deleteBucketCORSRules(bucketName);
// 關閉OSSClient。
ossClient.shutdown();