简介
本文档提供关于跨域访问的 API 概览以及 SDK 示例代码。
API | 操作名 | 操作描述 |
---|---|---|
PUT Bucket cors | 设置跨域配置 | 设置存储桶的跨域名访问权限 |
GET Bucket cors | 查询跨域配置 | 查询存储桶的跨域名访问配置信息 |
DELETE Bucket cors | 删除跨域配置 | 删除存储桶的跨域名访问配置信息 |
SDK API 参考
SDK 所有接口的具体参数与方法说明,请参考 SDK API。
设置跨域配置
功能说明
设置指定存储桶的跨域名访问配置信息(PUT Bucket cors)。
示例代码
try
{
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
//设置跨域访问配置 CORS
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);
//执行请求
PutBucketCORSResult result = cosXml.PutBucketCORS(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:更多完整示例,请前往 GitHub 查看。
查询跨域配置
功能说明
查询指定存储桶的跨域名访问配置信息(GET Bucket cors)。
示例代码
try
{
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
//执行请求
GetBucketCORSResult result = cosXml.GetBucketCORS(request);
//存储桶的 CORS 配置信息
CORSConfiguration conf = result.corsConfiguration;
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:更多完整示例,请前往 GitHub 查看。
删除跨域配置
功能说明
删除指定存储桶的跨域名访问配置(DELETE Bucket cors)。
示例代码
try
{
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
DeleteBucketCORSRequest request = new DeleteBucketCORSRequest(bucket);
//执行请求
DeleteBucketCORSResult result = cosXml.DeleteBucketCORS(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
说明:更多完整示例,请前往 GitHub 查看。