This topic describes how to use

For the complete code of upload callback, see GitHub.

Run the following code for upload callback:

// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
// Specify the IP address of the server you want to send the callback request to, for example, or
String callbackUrl = "<yourCallbackServerUrl>";

// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

String content = "Hello OSS";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName,new ByteArrayInputStream(content.getBytes()));

// Configure upload callback parameters.
Callback callback = new Callback();
// Configure the value of the host field carried in the callback request header, such as
// Configure the value of the body field carried in the callback request.
// Configure Content-Type for the callback request.
// Configure the custom parameters used to initiate a callback request. Each custom parameter consists of a key and a value. The key must start with x:.
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");

PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);

// Read the message returned from upload callback.
byte[] buffer = new byte[1024];
// If you do not close the reader after the data is read, connection leaks may occur. Consequently, no available connections are left and an exception occurs.

// Close your OSSClient instance.

For more information, see Upload callback in OSS Developer Guide.