Cross-origin Access

Last updated: 2023-09-13 11:24:24

Feature Overview

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

Setting a cross-origin access configuration

Note

Set the cross-origin access configuration information for a specified bucket (PUT Bucket cors). For more information about cross-origin access, please refer to Cross-Origin Access. For instructions on setting up cross-origin access through the console, please refer to Setting Cross-Origin Access or Best Practices for Cross-Origin Access.

Method prototype

public Guzzle\Service\Resource\Model putBucketCors(array $args = array());

Sample Request

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //Replace it with the actual <1>SecretId</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual <1>SecretKey</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual <1>region</1>, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //Protocol header, default is http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->putBucketCors(array(
'Bucket' => 'examplebucket-1250000000', // Bucket name, composed of BucketName-Appid, can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
'CORSRules' => array(
array(
'AllowedHeaders' => array('*',),
'AllowedMethods' => array('PUT', ),
'AllowedOrigins' => array('*', ),
'ExposeHeaders' => array('*', ),
'MaxAgeSeconds' => 1,
),
// ... repeated
)
));
// Request successful
print_r($result);
} catch (\Exception $e) {
// Request failed
echo "$e\n";
}

Description

Parameter name
Local Disk Types
Description
Required
Bucket
String
String
Required
CORSRules
Array
CORS configuration list
Required
CORSRule
Array
CORS configuration
Required
AllowedMethods
String
Allowed HTTP operations. Enumerated values: GET, PUT, HEAD, POST, DELETE
Required
AllowedOrigins
String
Allowed access sources support wildcard *, in the format: protocol://domain[:port], for example, http://www.qq.com.
Required
AllowedHeaders
String
Tells the server what custom HTTP request headers can be used for subsequent requests when the OPTIONS request is sent. Wildcard * is supported
Not required
ExposeHeaders
String
Custom header information that can be received by the browser from the server
Not required
MaxAgeSeconds
Int
Sets the validity period of the OPTIONS request result
Not required
ID
String
Rule ID
Required

Querying a Cross-Origin Configuration

Note

This API is used to query the cross-origin access configuration of a bucket.

Method prototype

public Guzzle\Service\Resource\Model getBucketCors(array $args = array());

Sample Request

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //Replace it with the actual <1>SecretId</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual <1>SecretKey</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual <1>region</1>, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //Protocol header, default is http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->getBucketCors(array(
'Bucket' => 'examplebucket-1250000000' // Bucket name, composed of BucketName-Appid, can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
));
// Request successful
print_r($result);
} catch (\Exception $e) {
// Request failed
echo($e);
}

Description

Parameter name
Local Disk Types
Description
Required
Bucket
String
Bucket for which CORS configuration is queried, in the format of BucketName-APPID
Required

Sample response

Guzzle\Service\Resource\Model Object
(
[data:protected] => Array
(
[CORSRules] => Array
(
[0] => Array
(
[ID] => 1234
[AllowedHeaders] => Array
(
[0] => *
)
[AllowedMethods] => Array
(
[0] => PUT
)
[AllowedOrigins] => Array
(
[0] => http://www.qq.com
)
)
)
[RequestId] => NWE3YzhkMmRfMTdiMjk0MGFfNTQzZl8xNWUw****
)
)

Response description

Parameter name
Local Disk Types
Description
Parent Node
CORSRules
Array
CORS configuration list
-
CORSRule
Array
CORS configuration
CORSRules
AllowedMethods
String
Allowed HTTP operations. Enumerated values: GET, PUT, HEAD, POST, DELETE
CORSRule
AllowedOrigins
String
Allowed access sources support wildcard *, in the format: protocol://domain[:port], for example, http://www.qq.com.
CORSRule
AllowedHeaders
String
Tells the server what custom HTTP request headers can be used for subsequent requests when the OPTIONS request is sent. Wildcard * is supported
CORSRule
ExposeHeaders
String
Custom header information that can be received by the browser from the server
CORSRule
MaxAgeSeconds
Int
Sets the validity period of the OPTIONS request result
CORSRule
ID
String
Rule ID
CORSRule

Deleting a Cross-Origin Configuration

Note

This API is used to delete the cross-origin access configuration of a bucket.

Method prototype

public Guzzle\Service\Resource\Model deleteBucketCors(array $args = array());

Sample Request

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //Replace it with the actual <1>SecretId</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual <1>SecretKey</1>, which can be viewed and managed in the CAM console at https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual <1>region</1>, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //Protocol header, default is http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$result = $cosClient->deleteBucketCors(array(
'Bucket' => 'examplebucket-1250000000' // Bucket name, composed of BucketName-Appid, can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket
));
// Request successful
print_r($result);
} catch (\Exception $e) {
// Request failed
echo($e);
}

Description

Parameter name
Local Disk Types
Description
Required
Bucket
String
Bucket for which CORS configuration is deleted, in the format of BucketName-APPID
Required