After Object Storage Service (OSS) completes simple upload by using put_object and put_object_from_file and multipart upload by using complete_multipart_upload, a callback request can be sent to the application server. To implement callback, you need only to send a request that contains relevant callback parameters to OSS.

Sample code

The following code provides an example on how to implement upload callback when you upload a string to the exampleobject.txt object in the examplefiles directory of the examplebucket bucket:

# -*- coding: utf-8 -*-
import json
import base64
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Define the function used to encode the callback parameter in Base64. 
def encode_callback(callback_params):
    cb_str = json.dumps(callback_params).strip()
    return oss2.compat.to_string(base64.b64encode(oss2.compat.to_bytes(cb_str)))

# Set the upload callback parameter. 
callback_params = {}
# Set the address of the server to which the callback request is sent. Example: 
callback_params['callbackUrl'] = ''
# Optional. Set the value of the Host field included in the callback request header. 
#callback_params['callbackHost'] = 'yourCallbackHost'
# Set the value of the body field included in the callback request. 
callback_params['callbackBody'] = 'bucket=${bucket}&object=${object}'
# Set Content-Type for the callback request. 
callback_params['callbackBodyType'] = 'application/x-www-form-urlencoded'
encoded_callback = encode_callback(callback_params)
# Configure custom parameters for the callback request. Each custom parameter consists of a key and a value. The key must start with x:. 
callback_var_params = {'x:my_var1': 'my_val1', 'x:my_var2': 'my_val2'}
encoded_callback_var = encode_callback(callback_var_params)

# Implement upload callback. 
params = {'x-oss-callback': encoded_callback, 'x-oss-callback-var': encoded_callback_var}
# Specify the string and the full path of the object. The full path of the object cannot contain the bucket name. 
result = bucket.put_object('examplefiles/exampleobject.txt', 'a'*1024*1024, params)


  • For more information about the complete sample code for upload callback, visit GitHub.
  • For more information about the API operation that is called to configure upload callback, see Callback.