This document provides an overview of APIs and SDK sample codes related to cross-origin access.
API | Operation | Description |
---|---|---|
PUT Bucket cors | Setting a cross-origin access configuration | Sets cross-origin access permissions for a bucket |
GET Bucket cors | Querying a cross-origin access configuration | Queries the cross-origin access configuration of a bucket |
DELETE Bucket cors | Deleting a cross-origin access configuration | Deletes the cross-origin access configuration from a bucket |
For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.
This API is used to set the cross-origin access configuration for a specified bucket.
try
{
String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
// Make cross-origin access configuration
COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule =
new COSXML.Model.Tag.CORSConfiguration.CORSRule();
corsRule.id = "corsconfigureId";
corsRule.maxAgeSeconds = 6000;
corsRule.allowedOrigins = new List<string>();
corsRule.allowedOrigins.Add("http://cloud.tencent.com");
corsRule.allowedMethods = new List<string>();
corsRule.allowedMethods.Add("PUT");
corsRule.allowedHeaders = new List<string>();
corsRule.allowedHeaders.Add("Host");
corsRule.exposeHeaders = new List<string>();
corsRule.exposeHeaders.Add("x-cos-meta-x1");
request.SetCORSRule(corsRule);
// Execute the request
PutBucketCORSResult result = cosXml.PutBucketCORS(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.
This API is used to query the cross-origin access configuration of a bucket.
try
{
String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
// Execute the request
GetBucketCORSResult result = cosXml.GetBucketCORS(request);
// Cross-access configuration of the bucket
CORSConfiguration conf = result.corsConfiguration;
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.
This API is used to delete the cross-origin access configuration from a bucket.
try
{
String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
DeleteBucketCORSRequest request = new DeleteBucketCORSRequest(bucket);
// Execute the request
DeleteBucketCORSResult result = cosXml.DeleteBucketCORS(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.
Was this page helpful?