Пример #1
0
        /// 设置存储桶跨域规则
        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
        }