public static void AsynPutBucketCORS(COSXML.CosXml cosXml, string bucket) { PutBucketCORSRequest request = new PutBucketCORSRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置cors COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule = new COSXML.Model.Tag.CORSConfiguration.CORSRule(); corsRule.id = "corsconfigure1"; corsRule.maxAgeSeconds = 6000; corsRule.allowedOrigin = "http://cloud.tencent.com"; corsRule.allowedMethods = new List <string>(); corsRule.allowedMethods.Add("PUT"); corsRule.allowedMethods.Add("DELETE"); corsRule.allowedMethods.Add("POST"); corsRule.allowedHeaders = new List <string>(); corsRule.allowedHeaders.Add("Host"); corsRule.allowedHeaders.Add("Authorizaiton"); corsRule.allowedHeaders.Add("User-Agent"); corsRule.exposeHeaders = new List <string>(); corsRule.exposeHeaders.Add("x-cos-meta-x1"); corsRule.exposeHeaders.Add("x-cos-meta-x2"); request.SetCORSRule(corsRule); ///执行请求 cosXml.PutBucketCORS(request, delegate(CosResult cosResult) { PutBucketCORSResult result = cosResult as PutBucketCORSResult; Console.WriteLine(result.GetResultInfo()); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.Message); } if (serverEx != null) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }); }
/// 设置存储桶跨域规则 public void PutBucketCors() { //.cssg-snippet-body-start:[put-bucket-cors] try { string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID 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()); } //.cssg-snippet-body-end }
public void putBucketCors() { //.cssg-snippet-body-start:[put-bucket-cors] CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(60000) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(40000) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(true) //设置默认 HTTPS 请求 .SetAppid("1253653367") //设置腾讯云账户的账户标识 APPID .SetRegion("ap-guangzhou") //设置一个默认的存储桶地域 .Build(); string secretId = Environment.GetEnvironmentVariable("COS_KEY"); //云 API 密钥 SecretId string secretKey = Environment.GetEnvironmentVariable("COS_SECRET"); //云 API 密钥 SecretKey long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "bucket-cssg-test-dotnet-1253653367"; //格式:BucketName-APPID PutBucketCORSRequest request = new PutBucketCORSRequest(bucket); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置跨域访问配置 CORS COSXML.Model.Tag.CORSConfiguration.CORSRule corsRule = new COSXML.Model.Tag.CORSConfiguration.CORSRule(); corsRule.id = "corsconfigureId"; corsRule.maxAgeSeconds = 6000; corsRule.allowedOrigin = "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); Assert.Null(clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.Null(serverEx); } //.cssg-snippet-body-end }