Help Center/ Object Storage Service/ SDK Reference/ Android/ Object Upload/ Setting a Callback for a Regular Upload
Updated on 2025-09-22 GMT+08:00

Setting a Callback for a Regular Upload

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

You can specify the callback parameters in an object upload (PutObject) request. After the object is successfully uploaded, OBS calls back the upload result to a specific server and returns the callback result to you.

This example sets a callback for a regular object upload (PutObject) request.

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";

// Create an ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

// Specify the bucket name and object name.
String objectKey = "exmapleObjectKey";
String bucketName = "exmaplebucket";

// Set the upload data stream.
PutObjectRequest putRequest = new PutObjectRequest();
putRequest.setObjectKey(objectKey);
putRequest.setBucketName(bucketName);
putRequest.setInput(new ByteArrayInputStream("example_putObject_callback".getBytes(StandardCharsets.UTF_8)));

// Set the upload callback.
Callback callback = new Callback();
callback.setCallbackBody("exampleCallbackBody");
callback.setCallbackHost("exampleCallbackHost");
callback.setCallbackUrl("exampleCallbackUrl");
callback.setCallbackBodyType("exampleCallbackBodyType");
putRequest.setCallback(callback);

PutObjectResult putResult = obsClient.putObject(putRequest);