Updated on 2026-07-21 GMT+08:00

Setting CORS Rules

If you have any questions during the development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

You can call ObsClient.SetBucketCors to set CORS rules for a bucket. If the bucket is configured with CORS rules, the newly set ones will overwrite the existing ones.

This example sets a CORS rule for bucket bucketname.

The sample code is as follows:

// Initialize configuration parameters.
ObsConfig config = new ObsConfig();
config.Endpoint = "https://your-endpoint";
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
string accessKey= Environment.GetEnvironmentVariable("AccessKeyID", EnvironmentVariableTarget.Machine);
string secretKey= Environment.GetEnvironmentVariable("SecretAccessKey", EnvironmentVariableTarget.Machine);
// Create an instance of ObsClient.
ObsClient client = new ObsClient(accessKey, secretKey, config);
// Set the CORS rules for a bucket.
try
{
    SetBucketCorsRequest request = new SetBucketCorsRequest();
    request.BucketName = "bucketname";
    request.Configuration = new CorsConfiguration();
    CorsRule rule = new CorsRule();
    rule.Id = "20170820";
    // Specify the origin of the cross-domain request.
    rule.AllowedOrigins.Add("http://www.a.com");
    rule.AllowedOrigins.Add("http://www.b.com");
    // Specify whether headers specified in Access-Control-Request-Headers in the OPTIONS request can be used.
    rule.AllowedHeaders.Add("x-obs-header");
    // Specify the request method, which can be GET, PUT, DELETE, POST, or HEAD.
    rule.AllowedMethods.Add(HttpVerb.HEAD);
    rule.AllowedMethods.Add(HttpVerb.PUT);
    rule.AllowedMethods.Add(HttpVerb.GET);
    rule.AllowedMethods.Add(HttpVerb.POST);
    rule.AllowedMethods.Add(HttpVerb.DELETE);
    // Specify response headers that users can access using application programs.
    rule.ExposeHeaders.Add("x-obs-test1");
    rule.ExposeHeaders.Add("x-obs-test2");
    rule.MaxAgeSeconds = 100;
    request.Configuration.Rules.Add(rule);
    SetBucketCorsResponse response = client.SetBucketCors(request);
    Console.WriteLine("Set bucket cors response: {0}", response.StatusCode);
}
catch (ObsException ex)
{
    Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
    Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}
  • AllowedOrigins, AllowedMethods, and AllowedHeaders, respectively, can contain up to one wildcard character (*). Wildcard characters (*) indicate that all origins, operations, or headers are allowed.