tencent cloud

masukan

Cross-Origin Resource Sharing

Terakhir diperbarui:2024-06-25 10:53:13

    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

    SDK API References

    For the parameters and method descriptions of all the APIs in the SDK, see SDK API Reference.

    Setting CORS Configuration

    Description

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

    Sample code

    // Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket
    String bucket = "examplebucket-1250000000";
    PutBucketCORSRequest putBucketCORSRequest = new PutBucketCORSRequest(bucket);
    
    CORSConfiguration.CORSRule corsRule = new CORSConfiguration.CORSRule();
    
    // Set the rule ID
    corsRule.id = "123";
    // Allowed origin in the format: `protocol://domain name[:port number]`. The wildcard * is supported
    corsRule.allowedOrigin = "https://cloud.tencent.com";
    // Set the validity period of the OPTIONS request result
    corsRule.maxAgeSeconds = 5000;
    
    List<String> methods = new LinkedList<>();
    methods.add("PUT");
    methods.add("POST");
    methods.add("GET");
    // Allowed HTTP methods. Enumerated values: GET, PUT, HEAD, POST, DELETE
    corsRule.allowedMethod = methods;
    
    List<String> headers = new LinkedList<>();
    headers.add("host");
    headers.add("content-type");
    // Notify the server which custom HTTP request headers are allowed for subsequent requests when an OPTIONS request is sent. The wildcard * is supported
    corsRule.allowedHeader = headers;
    
    List<String> exposeHeaders = new LinkedList<>();
    exposeHeaders.add("x-cos-meta-1");
    // Set custom header information that the browser can receive from the server
    corsRule.exposeHeader = exposeHeaders;
    
    putBucketCORSRequest.addCORSRule(corsRule);
    
    cosXmlService.putBucketCORSAsync(putBucketCORSRequest,
    new CosXmlResultListener() {
    @Override
    public void onSuccess(CosXmlRequest request, CosXmlResult result) {
    PutBucketCORSResult putBucketCORSResult = (PutBucketCORSResult) result;
    }
    
    // If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:
    // clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Querying CORS Configuration

    Description

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

    Sample code

    // Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket
    String bucket = "examplebucket-1250000000";
    GetBucketCORSRequest getBucketCORSRequest = new GetBucketCORSRequest(bucket);
    cosXmlService.getBucketCORSAsync(getBucketCORSRequest,
    new CosXmlResultListener() {
    @Override
    public void onSuccess(CosXmlRequest request, CosXmlResult result) {
    GetBucketCORSResult getBucketCORSResult = (GetBucketCORSResult) result;
    }
    
    // If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:
    // clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Deleting CORS Configuration

    Description

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

    Sample code

    // Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket
    String bucket = "examplebucket-1250000000";
    DeleteBucketCORSRequest deleteBucketCORSRequest =
    new DeleteBucketCORSRequest(bucket);
    cosXmlService.deleteBucketCORSAsync(deleteBucketCORSRequest,
    new CosXmlResultListener() {
    @Override
    public void onSuccess(CosXmlRequest request, CosXmlResult result) {
    DeleteBucketCORSResult deleteBucketCORSResult =
    (DeleteBucketCORSResult) result;
    }
    
    // If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:
    // clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.
    Hubungi Kami

    Hubungi tim penjualan atau penasihat bisnis kami untuk membantu bisnis Anda.

    Dukungan Teknis

    Buka tiket jika Anda mencari bantuan lebih lanjut. Tiket kami tersedia 7x24.

    Dukungan Telepon 7x24