Bu konuda, Object Storage Service (OSS) SDK for Python'ın nasıl başlatılacağı açıklanmaktadır.
Kullanım notları
OSS SDK for Python'ı başlatmadan önce erişim kimlik bilgilerini yapılandırmanız gerekir. Bu konuda, erişim kimlik bilgileri ortam değişkenlerinden alınır. Daha fazla bilgi için bkz. OSS SDK for Python 1,0 kullanarak erişim kimlik bilgilerini yapılandırma.
OSS bölgeleri ve uç noktaları hakkında bilgi edinmek istiyorsanız bkz. Bölgeler ve uç noktalar.
Bir RAM kullanıcısı için AccessKey çifti oluşturmak istiyorsanız bkz. AccessKey çifti oluşturma.
-
OSS SDK for Python'daki çoğu işlem oss2.Service ve oss2.Bucket sınıflarına dayanır.
oss2.Service sınıfı paketleri listelemek için kullanılır.
oss2.Bucket sınıfı nesneleri yüklemek, indirmek ve silmek ile paketleri yapılandırmak için kullanılır.
oss2.Service ve oss2.Bucket sınıflarını başlatırken paketin bulunduğu bölgenin uç noktasını belirtmeniz gerekir. oss2.Service sınıfı, paketlerle eşleştirilmiş özel alan adlarını kullanarak paketlere erişimi desteklemez.
Ön koşullar
İstemciyi yapılandırmadan önce bir RAM kullanıcısının AccessKey çiftini kullanarak ortam değişkenlerini yapılandırmış olmanız gerekir.
Linux
-
Ortam değişkeni yapılandırmalarını
~/.bashrcdosyasına eklemek için CLI'da aşağıdaki komutları çalıştırın:echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc-
Değişiklikleri uygulayın.
source ~/.bashrc -
Ortam değişkenlerinin etkili olup olmadığını kontrol edin:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
-
macOS
-
Varsayılan kabuk türünü görüntülemek için terminalde aşağıdaki komutu çalıştırın:
echo $SHELL-
Varsayılan kabuk türüne göre ortam değişkenlerini yapılandırın.
Zsh
-
Ortam değişkeni yapılandırmalarını
~/.zshrcdosyasına eklemek için aşağıdaki komutları çalıştırın:echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc -
Değişiklikleri uygulayın.
source ~/.zshrc -
Ortam değişkenlerinin etkili olup olmadığını kontrol edin:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
Bash
-
Ortam değişkeni yapılandırmalarını
~/.bash_profiledosyasına eklemek için aşağıdaki komutları çalıştırın:echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile -
Değişiklikleri uygulayın.
source ~/.bash_profile -
Ortam değişkenlerinin etkili olup olmadığını kontrol edin:
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
-
-
Windows
CMD
-
CMD'de aşağıdaki komutları çalıştırın:
setx OSS_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx OSS_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"-
Ortam değişkenlerinin etkili olup olmadığını kontrol edin:
echo %OSS_ACCESS_KEY_ID% echo %OSS_ACCESS_KEY_SECRET%
-
PowerShell
-
PowerShell'de aşağıdaki komutları çalıştırın:
[Environment]::SetEnvironmentVariable("OSS_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::SetEnvironmentVariable("OSS_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)-
Ortam değişkeninin etkili olup olmadığını kontrol edin:
[Environment]::GetEnvironmentVariable("OSS_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
-
Varsayılan örnekler
Bu bölümde, V4 imza algoritması ve V1 imza algoritması kullanılarak OSS SDK for Python'ın nasıl başlatılacağına ilişkin örnekler sunulmaktadır.
Aşağıdaki örnek kodun paketin genel uç noktasını ve RAM kullanıcısının AccessKey çiftini kullandığını unutmayın.
(Önerilir) V4 imza algoritmasını kullanma
(Önerilmez) V1 imza algoritmasını kullanma
Yaygın senaryolarda yapılandırma örnekleri
Bu bölümde yaygın senaryolara yönelik yapılandırma örnekleri sunulmaktadır. Örnek kod, varsayılan olarak V4 imza algoritmasını ve RAM kullanıcısının AccessKey çiftini kullanarak OSS SDK for Python'ı başlatır.
Dahili uç nokta kullanarak OSS SDK for Python'ı başlatma
Uygulamalarınız bir ECS örneğinde dağıtılıyorsa ve ECS örneğiyle aynı bölgedeki bir paketteki OSS verilerine sık sık erişmeniz gerekiyorsa ağ gecikme süresini ve bant genişliği maliyetlerini önemli ölçüde azaltmak için dahili uç nokta kullanın.
Aşağıdaki örnek kod, bir OSSClient örneğini yapılandırmak için dahili uç noktanın nasıl kullanılacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the 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-internal.aliyuncs.com.
endpoint = 'yourEndpoint'
# Specify the region of the endpoint. Example: cn-hangzhou.
region = 'cn-hangzhou'
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
Özel alan adı kullanarak OSS SDK for Python'ı başlatma
Farklı kullanım amaçları için birden fazla paketiniz varsa kaynakları daha iyi yönetmek ve organize etmek için her paket için farklı alt alan adları belirtebilirsiniz.
Aşağıdaki örnek kod, bir OSSClient örneğini yapılandırmak için özel alan adının nasıl kullanılacağını gösterir.
Önce özel alan adını paketin varsayılan alan adıyla eşleştirmeniz gerekir. Aksi takdirde hata bildirilir. Özel alan adını bir paketin varsayılan alan adıyla nasıl eşleştireceğiniz hakkında daha fazla bilgi için bkz. Özel alan adı aracılığıyla OSS'ye erişme.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
// Specify a custom domain name. Example: https://static.example.com.
endpoint = 'yourEndpoint'
# Specify the region of the endpoint. Example: cn-hangzhou.
region = 'cn-hangzhou'
# Specify the name of the bucket. Set is_cname to true to enable CNAME.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', is_cname=True, region=region)
Bağlantı zaman aşımı süresini yapılandırma
Aşağıdaki örnek kod, bağlantı zaman aşımı süresinin nasıl yapılandırılacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked. As a result, the security of all resources in your account is compromised. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the 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 region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"
# Specify the name of the bucket and set the connection timeout period to 30 seconds.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', connect_timeout=30,region=region)
CRC-64'ü devre dışı bırakma
Nesneleri yüklerken veya indirirken veri bütünlüğünü sağlamak için varsayılan olarak CRC-64 etkindir.
CRC-64'ü devre dışı bırakmanızı önermiyoruz. CRC-64'ü devre dışı bırakırsanız nesneleri yüklerken veya indirirken veri bütünlüğü etkilenebilir.
Aşağıdaki örnek kod, CRC-64'ün nasıl devre dışı bırakılacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked. As a result, the security of all resources in your account is compromised. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the 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 region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"
# Specify the name of the bucket and set enable_crc to False to disable CRC-64.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', enable_crc=False,region=region)
Bağlantı havuzu boyutunu belirtme
Aşağıdaki örnek kod, bağlantı havuzu boyutunun nasıl belirtileceğini gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# We recommend that you do not save access credentials in the project code. Otherwise, access credentials may be leaked. As a result, the security of all resources in your account is compromised. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the 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 region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"
# Specify the connection pool size. Default value: 10.
session = oss2.Session(pool_size=20)
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', session=session,region=region)
TLS sürümünü belirtme
Farklı Transport Layer Security (TLS) sürümlerinin farklı güvenlik ve performans özellikleri vardır. Senaryonuza göre bir TLS sürümü seçin.
TLS sürümünü OSS SDK for Python 2,18,1 veya sonraki sürümlerde belirtebilirsiniz.
Aşağıdaki örnek kod, TLS sürümünün 1,2 olarak nasıl ayarlanacağını gösterir:
# -*- coding: utf-8 -*-
import ssl
import oss2
from requests.adapters import HTTPAdapter
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Configure the SSL adapter.
class SSLAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
# Set the TLS version to 1.2.
kwargs["ssl_version"] = ssl.PROTOCOL_TLSv1_2
return super().init_poolmanager(*args, **kwargs)
# Create a session object and configure the adapter by using the session object.
session = oss2.Session(adapter=SSLAdapter())
# 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 region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', session=session, region=region)
# Upload an object.
bucket.put_object("example.txt", "hello")
oss2.Bucket sınıfı tarafından desteklenen parametreler
Aşağıdaki tabloda oss2.Bucket sınıfını başlatırken yapılandırabileceğiniz parametreler açıklanmaktadır.
|
Parametre |
Örnek |
Açıklama |
Yöntem |
|
is_cname |
True |
Uç noktanın özel bir alan adı olup olmadığını belirtir. Geçerli değerler:
|
oss2.Bucket(auth, cname, 'examplebucket', is_cname=True, region=region) |
|
session |
mytestsession |
Oturumun adı; yeni bir oturumun başlatıldığını belirtir. Varsayılan değer: None. Bu parametreyi mevcut bir oturumun adına ayarlarsanız oturum yeniden kullanılır. |
oss2.Bucket(auth, endpoint, 'examplebucket', session=oss2.Session(), region=region) |
|
connect_timeout |
30 |
Bağlantı zaman aşımı süresi. Varsayılan değer: 60. Birim: saniye. |
oss2.Bucket(auth, endpoint, 'examplebucket', connect_timeout=30, region=region) |
|
app_name |
mytool |
OSS SDK for Python'ı kullanan uygulamanın adı. Varsayılan olarak bu parametre boştur. Parametre boş değilse User-Agent alanında karşılık gelen değeri belirtin. Önemli
Dize bir HTTP üst bilgi değeri olarak geçirilir ve HTTP standartlarına uygun olmalıdır. |
oss2.Bucket(auth, endpoint, 'examplebucket', app_name='mytool', region=region) |
|
enable_crc |
False |
CRC-64'ün etkinleştirilip etkinleştirilmeyeceğini belirtir.
|
oss2.Bucket(auth, endpoint, 'examplebucket', enable_crc=False, region=region) |