This topic describes how to use upload callback.

For the complete code used to implement upload callback, visit GitHub.

You can use upload callback when you use put_object, put_object_from_file, and complete_multipart_upload.

The following code provides an example on how to use upload callback:

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

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Configure the upload callback parameter.
callback_dict = {}
# Configure the IP address of the server to which you want to send the callback request, for example, http://oss-demo.aliyuncs.com:23450 or http://127.0.0.1:9090.
callback_dict['callbackUrl'] = 'http://oss-demo.aliyuncs.com:23450'
# (Optional) Configure the value of the host field carried in the callback request header.
# callback_dict['callbackHost'] = 'yourCallbackHost'
# Configure the value of the body field carried in the callback request.
callback_dict['callbackBody'] = 'filename=${object}&size=${size}&mimeType=${mimeType}'
# Configure Content-Type for the callback request.
callback_dict['callbackBodyType'] = 'application/x-www-form-urlencoded'
# The callback parameter is in the JSON format and encoded with Base64.
callback_param = json.dumps(callback_dict).strip()
base64_callback_body = base64.b64encode(callback_param)
# Configure the header with the encoded parameter and send the header to OSS.
headers = {'x-oss-callback': base64_callback_body}

# Upload the object and perform upload callback.
result = bucket.put_object('<yourObjectName>', 'a'*1024*1024, headers)
		

For more information about upload callback, see upload callback in Developer Guide.