|
| 1 | +package com.qcloud.cos.demo; |
| 2 | + |
| 3 | +import com.qcloud.cos.COSClient; |
| 4 | +import com.qcloud.cos.ClientConfig; |
| 5 | +import com.qcloud.cos.auth.BasicCOSCredentials; |
| 6 | +import com.qcloud.cos.auth.COSCredentials; |
| 7 | +import com.qcloud.cos.model.BucketCrossOriginConfiguration; |
| 8 | +import com.qcloud.cos.model.CORSRule; |
| 9 | +import com.qcloud.cos.region.Region; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class BucketCorsDemo { |
| 15 | + private static String bucketName = "examplebucket-1250000000"; |
| 16 | + |
| 17 | + private static COSClient cosClient = createClient(); |
| 18 | + public static void main(String[] args) { |
| 19 | + try { |
| 20 | + setBucketCorsDemo(); |
| 21 | + getBucketCorsDemo(); |
| 22 | + deleteBucketCorsDemo(); |
| 23 | + } catch (Exception e) { |
| 24 | + e.printStackTrace(); |
| 25 | + } finally { |
| 26 | + cosClient.shutdown(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private static COSClient createClient() { |
| 31 | + // 1 初始化用户身份信息(appid, secretId, secretKey) |
| 32 | + COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY"); |
| 33 | + // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224 |
| 34 | + ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou")); |
| 35 | + // 3 生成cos客户端 |
| 36 | + COSClient cosclient = new COSClient(cred, clientConfig); |
| 37 | + |
| 38 | + return cosclient; |
| 39 | + } |
| 40 | + |
| 41 | + private static void setBucketCorsDemo() { |
| 42 | + BucketCrossOriginConfiguration bucketCORS = new BucketCrossOriginConfiguration(); |
| 43 | + List<CORSRule> corsRules = new ArrayList<CORSRule>(); |
| 44 | + CORSRule corsRule = new CORSRule(); |
| 45 | + // 规则名称 |
| 46 | + corsRule.setId("set-bucket-cors-test"); |
| 47 | + // 允许的 HTTP 方法 |
| 48 | + corsRule.setAllowedMethods(CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.GET, CORSRule.AllowedMethods.HEAD); |
| 49 | + corsRule.setAllowedHeaders("x-cos-grant-full-control"); |
| 50 | + corsRule.setAllowedOrigins("http://mail.qq.com", "http://www.qq.com", "http://video.qq.com"); |
| 51 | + corsRule.setExposedHeaders("x-cos-request-id"); |
| 52 | + corsRule.setMaxAgeSeconds(60); |
| 53 | + corsRules.add(corsRule); |
| 54 | + bucketCORS.setRules(corsRules); |
| 55 | + cosClient.setBucketCrossOriginConfiguration(bucketName, bucketCORS); |
| 56 | + System.out.println("finish set bucket cors"); |
| 57 | + } |
| 58 | + |
| 59 | + private static void getBucketCorsDemo() { |
| 60 | + BucketCrossOriginConfiguration bucketCrossOriginConfiguration = cosClient.getBucketCrossOriginConfiguration(bucketName); |
| 61 | + List<CORSRule> corsRules = bucketCrossOriginConfiguration.getRules(); |
| 62 | + for (CORSRule rule : corsRules) { |
| 63 | + List<CORSRule.AllowedMethods> allowedMethods = rule.getAllowedMethods(); |
| 64 | + List<String> allowedHeaders = rule.getAllowedHeaders(); |
| 65 | + List<String> allowedOrigins = rule.getAllowedOrigins(); |
| 66 | + List<String> exposedHeaders = rule.getExposedHeaders(); |
| 67 | + int maxAgeSeconds = rule.getMaxAgeSeconds(); |
| 68 | + System.out.println("allow methods:" + allowedMethods); |
| 69 | + System.out.println("allow headers:" + allowedHeaders); |
| 70 | + System.out.println("allow origins:" + allowedOrigins); |
| 71 | + System.out.println("exposed headers:" + exposedHeaders); |
| 72 | + System.out.println("max age seconds:" + maxAgeSeconds); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static void deleteBucketCorsDemo() { |
| 77 | + cosClient.deleteBucketCrossOriginConfiguration(bucketName); |
| 78 | + System.out.println("finish delete bucket cors"); |
| 79 | + } |
| 80 | +} |
0 commit comments