All Products
Search
Document Center

Object Storage Service:Exception handling

Last Updated:Aug 16, 2023

Object Storage Service (OSS) SDK for Python returns three types of exceptions: ClientError, RequestError, and ServerError. These exceptions are defined in the oss2.exceptions submodule.

The following table describes the variables of the exceptions.

Variable

Type

Description

status

int

  • If a ServerError exception occurs, an HTTP status code is returned for this variable.

  • If a ClientError or RequestError exception occurs, a fixed value is returned for this variable.

request_id

str

  • If a ServerError exception occurs, the OSS server returns the request ID for this variable.

  • If a ClientError or RequestError exception occurs, an empty string is returned for this variable.

code and message

str

The text in the Code and Message XML tags in an OSS error response. For more information, see Overview.

Example of exception handling

The following sample code provides an example on how to handle an exception and display the HTTP status code of the error and the request ID when you attempt to download an object that does not exist:

# -*- 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.ProviderAuth(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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

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 occurs due to incorrect input on the client. For example, the error occurs when you use the bucket.batch_delete_objects method and receive an empty object list. The value of status for ClientError is oss2.exceptions.OSS_CLIENT_ERROR_STATUS.

RequestError

When the HTTP library throws an error, OSS SDK for Python converts the error to RequestError. The value of status for RequestError is oss2.exceptions.OSS_REQUEST_ERROR_STATUS.

ServerError

When the OSS server returns an HTTP status code, OSS SDK for Python converts the status code to ServerError. ServerError derives multiple subclasses based on the HTTP status code and OSS error code. The NotFound subclass corresponds to HTTP status code 404. The Conflict subclass corresponds to HTTP status code 409.

The following table describes common error codes.

Exception class

HTTP status code

OSS error code

Description

NotModified

304

Null

The specified value of the If-Modified-Since parameter is later than the actual time when the object is modified.

InvalidArgument

400

InvalidArgument

The request body is specified in a multipart upload request in which you set x-oss-complete-all to yes. In this case, the request body cannot be specified. Otherwise, an error occurs.

AccessDenied

403

AccessDenied

You do not have the required permissions.

NoSuchBucket

404

NoSuchBucket

The specified bucket does not exist.

NoSuchKey

404

NoSuchKey

The specified object does not exist.

NoSuchUpload

404

NoSuchUpload

The object is not completely uploaded in a multipart upload or resumable upload request.

NoSuchWebsite

404

NoSuchWebsiteConfiguration

The specified bucket does not have static website hosting configurations.

NoSuchLifecycle

404

NoSuchLifecycle

The specified bucket has no lifecycle rules configured.

NoSuchCors

404

NoSuchCORSConfiguration

The specified bucket has no cross-origin resource sharing (CORS) rules configured.

BucketNotEmpty

409

BucketNotEmpty

The bucket to be deleted contains objects, parts generated by incomplete multipart upload tasks, or LiveChannels.

PositionNotEqualToLength

409

PositionNotEqualToLength

The value of position in the request does not match the current object size.

ObjectNotAppendable

409

ObjectNotAppendable

The current object is not an appendable object.