tencent cloud

Feedback

Cross-Origin Resource Sharing

Last updated: 2024-02-02 14:36:36

    Overview

    This document provides an overview of APIs and SDK sample codes related to cross-origin resource sharing (CORS).
    API
    Operation
    Description
    Setting CORS configuration
    Sets the CORS permissions of bucket
    Querying CORS configuration
    Queries the CORS configuration of a bucket
    Deleting CORS configuration
    Deletes the CORS configuration of a bucket

    Setting CORS Configuration

    Description

    This API is used to set the CORS configuration of a specified bucket.

    Sample code

    try
    {
    // Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
    string bucket = "examplebucket-1250000000";
    PutBucketCORSRequest request = new PutBucketCORSRequest(bucket);
    // Set CORS 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.

    Querying CORS Configuration

    Description

    This API is used to query the CORS configuration of a bucket.

    Sample code

    try
    {
    // Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
    string bucket = "examplebucket-1250000000";
    GetBucketCORSRequest request = new GetBucketCORSRequest(bucket);
    // Execute the request
    GetBucketCORSResult result = cosXml.GetBucketCORS(request);
    // Bucket CORS configuration
    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.

    Deleting CORS Configuration

    Description

    This API is used to delete the CORS configuration of a bucket.

    Sample code

    try
    {
    // Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
    string bucket = "examplebucket-1250000000";
    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.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support