You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage bucket configuration
Manage cross-origin resource sharing (Python SDK)
Copy page
Download PDF
Manage cross-origin resource sharing (Python SDK)
Set Cross-Origin Resource Sharing (CORS) rules. When TOS receives a cross-origin request or OPTIONS request, it will read the CORS rules corresponding to the bucket, perform corresponding permission checks and return corresponding headers to ensure the security of cross-origin data transmission. This article describes how to configure CORS for buckets.​
Set CORS rules​
warning​
To set CORS rules for buckets, you must have the tos: PutBucketCORS permission. For more information, see Overview.​
Sample code​
The following code sets the CORS rules for bucket-test.​
​
import os​
import tos​
​
from tos.models2 import CORSRule​
​
# Retrieve AKSK information from environment variables.​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
endpoint = "your endpoint"​
region = "your region"​
bucket_name = "bucket-test"​
​
try:​
# Create a TosClientV2 object to perform operations on buckets and objects.​
client = tos.TosClientV2(ak, sk, endpoint, region)​
​
rule = CORSRule(allowed_origins=['*'],​
allowed_methods=['GET', 'HEAD'],​
allowed_headers=['*'],​
max_age_seconds=1000)​
​
# The existing rules will be overridden.​
client.put_bucket_cors(bucket_name, [rule])​
except tos.exceptions.TosClientError as e:​
# Operation failed, catch client error, usually caused by invalid request parameters or network issues​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# Operation failed, catch server error, detailed error information can be obtained from the response​
print('fail with server error, code: {}'.format(e.code))​
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
Get CORS rules​
warning​
To obtain CORS rules for buckets, you must have the tos: GetBucketCORS permission. For more information, see Overview.​
Sample code​
The following code is used to obtain the CORS rules for bucket-test.​
​
import os​
import tos​
​
from tos.models2 import CORSRule​
​
# Retrieve AKSK information from environment variables.​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
endpoint = "your endpoint"​
region = "your region"​
bucket_name = "bucket-test"​
​
try:​
client = tos.TosClientV2(ak, sk, endpoint, region)​
# If no CORS rules are set, a tos.exceptions.TosServerError will be thrown.​
cors = client.get_bucket_cors(bucket_name)​
for rule in cors.cors_rules:​
print('AllowedOrigins={}'.format(rule.allowed_origins))​
print('AllowedMethods={}'.format(rule.allowed_methods))​
print('AllowedHeaders={}'.format(rule.allowed_headers))​
print('ExposeHeaders={}'.format(rule.expose_headers))​
print('MaxAgeSeconds={}'.format(rule.max_age_seconds))​
except tos.exceptions.TosClientError as e:​
# Operation failed, catch client error, usually caused by invalid request parameters or network issues​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# Operation failed, catch server error, detailed error information can be obtained from the response​
print('fail with server error, code: {}'.format(e.code))​
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
Delete CORS rules​
warning​
To remove a CORS rule for a bucket, you must have the tos: PutBucketCORS permission. For more information, see Overview.​
Sample code​
The following code deletes the CORS rule for the specified bucket.​
​
import os​
import tos​
​
# Retrieve AKSK information from environment variables.​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
endpoint = "your endpoint"​
region = "your region"​
bucket_name = "bucket-test"​
​
try:​
# Create a TosClientV2 object to perform operations on buckets and objects​
client = tos.TosClientV2(ak, sk, endpoint, region)​
# Delete the CORS configuration for a bucket​
client.delete_bucket_cors(bucket_name)​
except tos.exceptions.TosClientError as e:​
# Operation failed, catch client error, usually caused by invalid request parameters or network issues​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# Operation failed, catch server error, detailed error information can be obtained from the response​
print('fail with server error, code: {}'.format(e.code))​
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
Related documentation​
For more information on cross-origin resource sharing, see CORS rules.​
Last updated: 2026.08.07 17:25:56