Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:CORS (OSS SDK for Python 1.0)

Son Güncelleme:Oct 01, 2026

Tarayıcıların aynı kaynak ilkesi nedeniyle farklı alan adları arasında veri alışverişi yapılırken veya kaynaklar paylaşılırken çapraz kaynak istekleri reddedilebilir. Bu sorunu çözmek için çapraz kaynak kaynağı paylaşımı (CORS) kurallarını yapılandırabilirsiniz. CORS kurallarında istek gönderilebilecek alan adlarını, çapraz kaynak isteklerini göndermek için kullanılabilecek yöntemleri ve izin verilen üst bilgileri belirtebilirsiniz.

Notlar

  • Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. OSS SDK for Python 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin oluşturulmasını göstermektedir. Özel bir alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma.

  • CORS kurallarını yapılandırmak için oss:PutBucketCors iznine sahip olmanız gerekir. CORS kurallarını sorgulamak için oss:GetBucketCors iznine sahip olmanız gerekir. CORS kurallarını silmek için oss:DeleteBucketCors iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel bir ilke atama.

CORS kurallarını yapılandırma

Aşağıdaki örnek kod, belirli bir paket için bir CORS kuralının nasıl yapılandırılacağını gösterir:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketCors, CorsRule

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

rule = CorsRule(allowed_origins=['*'],
                allowed_methods=['GET', 'HEAD'],
                allowed_headers=['*'],
                max_age_seconds=1000)

# An existing rule with the same name is overwritten. 
bucket.put_bucket_cors(BucketCors([rule]))            

CORS kurallarını sorgulama

Aşağıdaki örnek kod, belirli bir paketin CORS kurallarının nasıl sorgulanacağını gösterir:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

try:
    cors = bucket.get_bucket_cors()
except oss2.exceptions.NoSuchCors:
    print('cors is not set')
else:
    for rule in cors.rules:
        print('AllowedOrigins={0}'.format(rule.allowed_origins))
        print('AllowedMethods={0}'.format(rule.allowed_methods))
        print('AllowedHeaders={0}'.format(rule.allowed_headers))
        print('ExposeHeaders={0}'.format(rule.expose_headers))
        print('MaxAgeSeconds={0}'.format(rule.max_age_seconds))            

CORS kurallarını silme

Aşağıdaki örnek kod, belirli bir paketin tüm CORS kurallarının nasıl silineceğini gösterir:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

bucket.delete_bucket_cors()            

İlgili kaynaklar

  • CORS kurallarını yönetmeye ilişkin tam örnek kod için GitHub adresini ziyaret edin.

  • CORS kurallarını yapılandırmak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketCors.

  • CORS kurallarını sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketCors.

  • CORS kurallarını silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketCors.