Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
164 lines (127 sloc) 4.98 KB

跨域资源共享 {#concept_89705_zh .concept}

本文介绍如何进行跨域资源共享。

跨域资源共享(Cross-origin resource sharing,简称CORS)允许Web端的应用程序访问不属于本域的资源。OSS提供跨域资源共享接口,方便您控制跨域访问的权限。

更多关于跨域资源共享的介绍,请参见开发指南中的设置跨域访问和API参考中[PutBucketcors](../../../../../cn.zh-CN/API 参考/跨域资源共享/PutBucketcors.md#)。

设置跨域资源共享规则 {#section_rpy_mjz_kfb .section}

以下代码用于设置指定存储空间的跨域资源共享规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
      /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
    SetBucketCorsRequest request(BucketName);
  
    /* 设置跨域资源共享规则 */
    auto rule1 = CORSRule();
    /* 指定允许跨域请求的来源 */
    rule1.addAllowedOrigin("http://www.a.com");
    /* 指定允许跨域请求方法(GET/PUT/POST/DELETE/HEAD)*/
    rule1.addAllowedMethod("POST");
    /* 是否允许预取指令(OPTIONS)中Access-Control-Request-Headers头中指定的Header */
    rule1.addAllowedHeader("*");
    /* 指定允许用户从应用程序中访问的响应头 */
    rule1.addExposeHeader("x-oss-test");
    /* 最多允许10条规则 */
    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()) {
        /* 异常处理 */
        std::cout << "SetBucketCors fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

查看跨域资源共享规则 {#section_yb3_vnl_ngb .section}

以下代码用于查看跨域资源共享规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, c onf);
  
 
    /* 查看跨域资源共享规则 */
    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 {
        /* 异常处理 */
        std::cout << "GetBucketCors fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

删除跨域资源共享规则 {#section_zb3_vnl_ngb .section}

以下代码用于删除指定存储空间的所有跨域资源共享规则:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
 
    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf);
 
 
    /* 删除跨域资源共享规则 */
    auto outcome = client.DeleteBucketCors(BucketName);
  
    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "DeleteBucketCors fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}
You can’t perform that action at this time.