This topic describes how to perform cross-origin resource sharing (CORS).

CORS allows Web applications to access resources that belong to another region. OSS provides CORS APIs to facilitate cross-origin access control.

For more information, see the "CORS" and "PutBucketcors" sections in OSS Developer Guide.

Configure CORS rules

Use the following code to configure CORS rules for a specified bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
      /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
    SetBucketCorsRequest request(BucketName);
  
    /* Configure CORS rules. */
    auto rule1 = CORSRule();
    /* Specify the cross-origin source to share resources.*/
    rule1.addAllowedOrigin("http://www.a.com");
    /* Specify the allowed CORS request method. (You can select GET, PUT, POST, DELETE or HEAD.)*/
    rule1.addAllowedMethod("POST");
    /* Determine whether to allow the header specified in Access-Control-Request-Headers of the pre-flight (OPTIONS) request. */
    rule1.addAllowedHeader("*");
    /* Specify the response header that allows user access from applications. */
    rule1.addExposeHeader("x-oss-test");
    /* You can set a maximum of 10 rules. */
    request.addCORSRule(rule1);
    
    auto rule2 = CORSRule();
    rule2.addAllowedOrigin("http://www.b.com");
    rule2.addAllowedMethod("GET");
    rule2.addExposeHeader("x-oss-test2");
    rule2.setMaxAgeSeconds(100);
    request.addCORSRule(rule2);

    auto outcome = client.SetBucketCors(request);
  
    if (! outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "SetBucketCors fail" <<
        ",code:" << outcome.error(). Code() <<
        ",message:" << outcome.error(). Message() <<
        ",requestId:" << outcome.error(). RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

View CORS rules

Use the following code to view CORS rules set for a bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, c onf);
  
 
    /* View CORS rules. */
    auto outcome = client.GetBucketCors(BucketName);
  
    if (outcome.isSuccess()) {
        std::cout << "GetBucketCors success" << std::endl;
        for (auto const rule : outcome.result(). CORSRules()) {
            std::cout << "Get Bucket Cors List:" << std::endl;
            for (auto const origin : rule.AllowedOrigins()) {
                std::cout << "Allowed origin:" << origin << std::endl;
            }
        }
    }
    else {
        /* Handle exceptions. */
        std::cout << "GetBucketCors fail" <<
        ",code:" << outcome.error(). Code() <<
        ",message:" << outcome.error(). Message() <<
        ",requestId:" << outcome.error(). RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Delete CORS rules

Use the following code to delete the CORS rules set for a specified bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf);
 
 
    /* Delete the CORS rules. */
    auto outcome = client.DeleteBucketCors(BucketName);
  
    if (! outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "DeleteBucketCors fail" <<
        ",code:" << outcome.error(). Code() <<
        ",message:" << outcome.error(). Message() <<
        ",requestId:" << outcome.error(). RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}