Object Storage Service (OSS) paketiniz için bir Referer izin listesi veya engelleme listesi yapılandırabilir ve boş Referer içeren isteklere izin verilip verilmeyeceğini belirtebilirsiniz. Bu sayede paketteki kaynaklara yetkisiz erişimi önleyebilir ve beklenmeyen trafik ücretlerinden kaçınabilirsiniz.
Kullanım notları
Hotlink korumasını yapılandırmadan önce bu özelliği iyice anlayın. Daha fazla bilgi için bkz. Hotlink protection.
Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Regions and endpoints.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Configure access credentials using OSS SDK for Python 1.0.
Bu konu, bir OSS uç noktasıyla bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Initialization.
Hotlink korumasını yapılandırmak için
oss:PutBucketRefereriznine sahip olmanız gerekir. Hotlink koruma yapılandırmalarını sorgulamak içinoss:GetBucketRefereriznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Grant a custom policy.
Paket için hotlink koruması yapılandırma
Aşağıdaki örnek kod, bir paket için hotlink koruması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 BucketReferer
# Obtain access credentials from environment variables. Before you run the code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
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)
# Specify that empty Referer fields are allowed.
allow_empty_referer = True
# Specify a Referer whitelist.
referers = ['http://www.aliyun.com', 'https://www.aliyun.com']
# Specify a Referer blacklist.
# black_referers = ['http://example.com', 'http://*.example.com']
# Specify that query strings can be truncated.
allow_truncate_query_string = True
# Configure hotlink protection.
bucket.put_bucket_referer(BucketReferer(allow_empty_referer=allow_empty_referer, referers=referers, black_referers=black_referers,allow_truncate_query_string=allow_truncate_query_string))
Paketin hotlink koruma yapılandırmalarını sorgulama
Aşağıdaki örnek kod, bir paketin hotlink koruma yapılandırmalarının nasıl sorgulanacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
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)
config = bucket.get_bucket_referer()
print('allow empty referer={0}, referers={1}'.format(config.allow_empty_referer, config.referers))
Hotlink koruma yapılandırmalarını temizleme
Aşağıdaki örnek kod, bir paketin hotlink koruma yapılandırmalarının nasıl silineceğini gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.models import BucketReferer
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
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)
# The hotlink protection configurations of a bucket cannot be directly cleared. To overwrite the existing hotlink protection configurations, you must configure a new hotlink protection rule that allows requests with an empty Referer header.
bucket.put_bucket_referer(BucketReferer(True, []))
İlgili konular
Bir paket için hotlink koruması yapılandırmak üzere çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketReferer.
Bir paketin hotlink koruma yapılandırmalarını sorgulamak üzere çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketReferer.