This document provides an overview of APIs and SDK sample codes related to cross-origin resource sharing (CORS).
API | Operation | Description |
---|---|---|
PUT Bucket cors | Setting CORS configuration | Sets the CORS permissions of bucket |
GET Bucket cors | Querying CORS configuration | Queries the CORS configuration of a bucket |
DELETE Bucket cors | Deleting CORS configuration | Deletes the CORS configuration of a bucket |
For the parameters and method descriptions of all the APIs in the SDK, see SDK API Reference.
This API is used to set the CORS configuration of a specified 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/bucket
String bucket = "examplebucket-1250000000";
PutBucketCORSRequest putBucketCORSRequest = new PutBucketCORSRequest(bucket);
CORSConfiguration.CORSRule corsRule = new CORSConfiguration.CORSRule();
// Set the rule ID
corsRule.id = "123";
// Allowed origin in the format: `protocol://domain name[:port number]`. The wildcard * is supported
corsRule.allowedOrigin = "https://cloud.tencent.com";
// Set the validity period of the OPTIONS request result
corsRule.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, DELETE
corsRule.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 supported
corsRule.allowedHeader = headers;
List<String> exposeHeaders = new LinkedList<>();
exposeHeaders.add("x-cos-meta-1");
// Set custom header information that the browser can receive from the server
corsRule.exposeHeader = exposeHeaders;
putBucketCORSRequest.addCORSRule(corsRule);
cosXmlService.putBucketCORSAsync(putBucketCORSRequest,
new CosXmlResultListener() {
@Override
public 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?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:For more samples, please visit GitHub.
This API is used to query 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/bucket
String bucket = "examplebucket-1250000000";
GetBucketCORSRequest getBucketCORSRequest = new GetBucketCORSRequest(bucket);
cosXmlService.getBucketCORSAsync(getBucketCORSRequest,
new CosXmlResultListener() {
@Override
public 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?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:For more samples, please visit GitHub.
This API is used to delete 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/bucket
String bucket = "examplebucket-1250000000";
DeleteBucketCORSRequest deleteBucketCORSRequest =
new DeleteBucketCORSRequest(bucket);
cosXmlService.deleteBucketCORSAsync(deleteBucketCORSRequest,
new CosXmlResultListener() {
@Override
public 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?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:For more samples, please visit GitHub.
Apakah halaman ini membantu?