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, OSS sends a callback to the application server. To configure upload callbacks, you need to only add the required callback parameters to the upload request that is sent to OSS.
Usage notes
- In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
- In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or STS, see Initialization.
Examples
The following code provides an example on how to specify upload callbacks 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
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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.
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# Specify the function that is used to encode the callback parameters 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)))
# Specify the upload callback parameters.
callback_params = {}
# Specify the address of the callback server that receives the callback request. Example: http://oss-demo.aliyuncs.com:23450.
callback_params['callbackUrl'] = 'http://oss-demo.aliyuncs.com:23450'
# (Optional) Specify the Host field included in the callback request header.
#callback_params['callbackHost'] = 'yourCallbackHost'
# Specify the body field included in the callback request.
callback_params['callbackBody'] = 'bucket=${bucket}&object=${object}'
# Specify Content-Type in 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)
# Specify upload callback parameters.
params = {'x-oss-callback': encoded_callback, 'x-oss-callback-var': encoded_callback_var}
# Specify the full path of the object and the string that you want to upload. The path cannot contain the bucket name.
result = bucket.put_object('examplefiles/exampleobject.txt', 'a'*1024*1024, params)