API | Operation | Description |
Setting CORS configuration | Sets the CORS permissions of bucket | |
Querying CORS configuration | Queries the CORS configuration of a bucket | |
Deleting CORS configuration | Deletes the CORS configuration of a bucket |
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";PutBucketCORSRequest putBucketCORSRequest = new PutBucketCORSRequest(bucket);CORSConfiguration.CORSRule corsRule = new CORSConfiguration.CORSRule();// Set the rule IDcorsRule.id = "123";// Allowed origin in the format: `protocol://domain name[:port number]`. The wildcard * is supportedcorsRule.allowedOrigin = "https://cloud.tencent.com";// Set the validity period of the OPTIONS request resultcorsRule.maxAgeSeconds = 5000;List<String> methods = new LinkedList<>();methods.add("PUT");methods.add("POST");methods.add("GET");// Allowed HTTP methods. Enumerated values: GET, PUT, HEAD, POST, DELETEcorsRule.allowedMethod = methods;List<String> headers = new LinkedList<>();headers.add("host");headers.add("content-type");// Notify the server which custom HTTP request headers are allowed for subsequent requests when an OPTIONS request is sent. The wildcard * is supportedcorsRule.allowedHeader = headers;List<String> exposeHeaders = new LinkedList<>();exposeHeaders.add("x-cos-meta-1");// Set custom header information that the browser can receive from the servercorsRule.exposeHeader = exposeHeaders;putBucketCORSRequest.addCORSRule(corsRule);cosXmlService.putBucketCORSAsync(putBucketCORSRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {PutBucketCORSResult putBucketCORSResult = (PutBucketCORSResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";GetBucketCORSRequest getBucketCORSRequest = new GetBucketCORSRequest(bucket);cosXmlService.getBucketCORSAsync(getBucketCORSRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {GetBucketCORSResult getBucketCORSResult = (GetBucketCORSResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";DeleteBucketCORSRequest deleteBucketCORSRequest =new DeleteBucketCORSRequest(bucket);cosXmlService.deleteBucketCORSAsync(deleteBucketCORSRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {DeleteBucketCORSResult deleteBucketCORSResult =(DeleteBucketCORSResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Apakah halaman ini membantu?