简介
本文档提供关于跨域访问的 API 概览以及 SDK 示例代码。
相关示例
功能名称 | 描述 | 示例代码 |
设置跨域配置 | 设置存储桶的跨域名访问权限 | |
查询跨域配置 | 查询存储桶的跨域名访问配置信息 | |
删除跨域配置 | 删除存储桶的跨域名访问配置信息 |
前期准备:初始化 COS 服务实例
public class AccessManageModel { private CosXml cosXml; //将服务用户设置成数据成员 // 初始化COS服务实例 private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }
使用案例
设置跨域配置
public void PutBucketCors() { 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()); } }
查询跨域配置
查询指定存储桶的跨域名访问配置信息(GET Bucket cors)。
public void GetBucketCors() { 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()); } }
删除跨域配置
删除指定存储桶的跨域名访问配置(DELETE Bucket cors)。
public void DeleteBucketCors() { 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()); } }
API 操作
关于设置跨域配置的 API 接口说明,请参见 PUT Bucket cors 文档。
关于查询跨域配置的 API 接口说明,请参见 GET Bucket cors 文档。
关于删除跨域配置的 API 接口说明,请参见 DELETE Bucket cors 文档。