OSS Python SDK exceptions (OssError) fall into three categories: ClientError, RequestError, and ServerError. These exceptions are defined in the oss2.exceptions submodule.
The following table describes the variables and types for exceptions.
Variable | Type | Description |
status | int |
|
request_id | str |
|
code and message | str | The text from the `Code` and `Message` XML tags in the OSS error response format. |
Exception handling example
The following code shows how to handle an exception when you download a file that does not exist. It prints the HTTP status code and request ID from the error message.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that corresponds to the Endpoint, for example, cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
try:
# Set the name of the file to download, for 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
A ClientError is caused by invalid client input. For example, this exception is thrown if you provide an empty file list to the `bucket.batch_delete_objects` method. The `status` value for a ClientError is `oss2.exceptions.OSS_CLIENT_ERROR_STATUS`.
RequestError
When the HTTP library throws an exception, the Python SDK converts it to a RequestError. The `status` value for a RequestError is `oss2.exceptions.OSS_REQUEST_ERROR_STATUS`.
ServerError
When the OSS server returns an HTTP error code, the Python SDK converts it to a ServerError. ServerError has multiple child classes that are derived from HTTP status codes and OSS error codes. For example, the NotFound child class corresponds to all 404 exceptions, and the Conflict child class corresponds to all 409 exceptions.
The following table lists common error codes.
Exception class | HTTP status code | OSS error code | Description |
NotModified | 304 | Empty | When you use conditional download, the time specified by the `If-Modified-Since` parameter is later than the actual modification time of the object. |
InvalidArgument | 400 | InvalidArgument | During a multipart upload, you cannot specify a body if `x-oss-complete-all:yes` is also specified. If you do, an error occurs. |
AccessDenied | 403 | AccessDenied | You do not have the required access permissions. |
NoSuchBucket | 404 | NoSuchBucket | The bucket does not exist. |
NoSuchKey | 404 | NoSuchKey | The file does not exist. |
NoSuchUpload | 404 | NoSuchUpload | During a multipart upload or resumable upload, some parts are uploaded successfully, but the upload is not completed. |
NoSuchWebsite | 404 | NoSuchWebsiteConfiguration | Static website hosting is not configured for the bucket. |
NoSuchLifecycle | 404 | NoSuchLifecycle | No lifecycle rule is configured for the bucket. |
NoSuchCors | 404 | NoSuchCORSConfiguration | Cross-origin resource sharing (CORS) is not configured for the bucket. |
BucketNotEmpty | 409 | BucketNotEmpty | The bucket you want to delete contains objects, incomplete multipart upload tasks, or LiveChannels that have not been deleted. |
PositionNotEqualToLength | 409 | PositionNotEqualToLength | The value of `Position` does not match the length of the current object. |
ObjectNotAppendable | 409 | ObjectNotAppendable | The current file is not an Appendable object. |