• Object Storage Service

  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Quick Start
  5. General Examples of ObsClient

General Examples of ObsClient

Each time an API is called by using ObsClient (without any exception thrown), an SDK common result object will be returned. You can obtain the HTTP status code from this object to check whether the operation is successful.

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    

    # Call an API to perform related operations, for example, uploading an object.
    resp = obsClient.putFile('bucketname', 'object', 'localfile')
    # If no exception occurs and the API call is complete, check the HTTP status code.
    if resp.status < 300:    
        # The operation is successful
        print('requestId:', resp.requestId)
        # Process the business logic after the operation is successful.
        # If the operation fails, obtain the exception details.
        print('errorCode:', resp.errorCode)    
        print('errorMessage:', resp.errorMessage)
        print('requestId:', resp.requestId)
        print('hostId:', resp.hostId)
except Exception as e:
    import traceback
    # If any error occurs, print the exception stacks. 
    # Close obsClient.