Object Storage Service (OSS) SDK for Python, üç tür özel durum döndürür: ClientError, RequestError ve ServerError. Bu özel durumlar oss2.exceptions alt modülünde tanımlanmıştır.
Aşağıdaki tabloda özel durum değişkenleri açıklanmaktadır.
|
Değişken |
Tür |
Açıklama |
|
status |
int |
|
|
request_id |
str |
|
|
code ve message |
str |
Bir OSS hata yanıtındaki Code ve Message XML etiketlerindeki metin. Daha fazla bilgi için bkz. Hata yanıtları. |
Özel durum işleme örneği
Aşağıdaki örnek kod, mevcut olmayan bir nesneyi indirmeye çalıştığınızda hatanın HTTP durum kodunu ve istek kimliğini görüntülemek üzere bir özel durumun nasıl işleneceğ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 the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
try:
# Specify the name of the downloaded object. Example: exampleobject.txt.
stream = bucket.get_object('exampleobject.txt')
except oss2.exceptions.NoSuchKey as e:
print('status={0}, request_id={1}'.format(e.status, e.request_id))
ClientError
ClientError, istemcideki hatalı giriş nedeniyle oluşur. Örneğin, bucket.batch_delete_objects yöntemini kullandığınızda ve boş bir nesne listesi aldığınızda bu hata meydana gelir. ClientError için status değeri oss2.exceptions.OSS_CLIENT_ERROR_STATUS'tır.
RequestError
HTTP kitaplığı bir hata verdiğinde OSS SDK for Python bu hatayı RequestError'a dönüştürür. RequestError için status değeri oss2.exceptions.OSS_REQUEST_ERROR_STATUS'tır.
ServerError
OSS sunucusu bir HTTP durum kodu döndürdüğünde OSS SDK for Python bu durum kodunu ServerError'a dönüştürür. ServerError, HTTP durum koduna ve OSS hata koduna göre birden çok alt sınıf türetir. NotFound alt sınıfı HTTP durum kodu 404'e karşılık gelir. Conflict alt sınıfı HTTP durum kodu 409'a karşılık gelir.
Aşağıdaki tabloda yaygın hata kodları açıklanmaktadır.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|