tencent cloud

Cloud Object Storage

Cross-Origin Resource Sharing

Download
Modo Foco
Tamanho da Fonte
Última atualização: 2026-05-15 15:33:56

Introduction

This document provides an overview of cross-domain access APIs and SDK sample code.
API
Operation Name
Operation Description
Setting CORS Configuration
Set the cross-origin access permission of a bucket.
Querying CORS Configuration
Query the cross-origin access configuration information of a bucket.
Deleting CORS Configuration
Delete the cross-origin access configuration information of a bucket.

SDK API Reference

For detailed parameters and method descriptions of all SDK interfaces, see SDK API Reference.

Setting CORS Configuration

Feature Overview

Configure cross-origin access for the specified bucket (PUT Bucket cors). For more information on cross-origin access, see Cross-Origin Access. For steps to configure cross-origin access via the console, see Configure Cross-Origin Access or Cross-Origin Access Best Practices.

Example Code

// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
PutBucketCORSRequest putBucketCORSRequest = new PutBucketCORSRequest(bucket);

CORSConfiguration.CORSRule corsRule = new CORSConfiguration.CORSRule();

// Configure the rule ID
corsRule.id = "123";
// Allowed origins, which support the wildcard *. The format is: protocol://domain[:port]
corsRule.allowedOrigin = "https://cloud.tencent.com";
// Set the validity period for results returned by OPTIONS requests
corsRule.maxAgeSeconds = 5000;

List<String> methods = new LinkedList<>();
methods.add("PUT");
methods.add("POST");
methods.add("GET");
// Allowed HTTP operations, for example: GET, PUT, HEAD, POST, DELETE
corsRule.allowedMethod = methods;

List<String> headers = new LinkedList<>();
headers.add("host");
headers.add("content-type");
// When sending an OPTIONS request, inform the server of the HTTP request headers that can be used in subsequent requests. These headers support the wildcard *.
corsRule.allowedHeader = headers;

List<String> exposeHeaders = new LinkedList<>();
exposeHeaders.add("x-cos-meta-1");
// Set custom headers from the server that the browser can receive
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 call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note
For more complete examples, see GitHub.

Querying CORS Configuration

Feature Overview

Query the cross-origin access configuration information of the specified bucket (GET Bucket cors).

Example Code

// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. 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 call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note
For more complete examples, see GitHub.

Deleting CORS Configuration

Feature Overview

Delete the cross-origin access configuration of the specified bucket (DELETE Bucket cors).

Example Code

// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. 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 call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note
For more complete examples, see GitHub.

Ajuda e Suporte

Esta página foi útil?

comentários