After an object is uploaded, Object Storage Service (OSS) can send 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. This topic describes how to configure upload callbacks.
Compared with simple upload, object upload with a callback configured requires the client to wait for a longer period of time to process a request and return a response.
Object upload and upload callback are asynchronously performed, the local file is uploaded to OSS before the upload callback is performed. Therefore, the local file is uploaded even if the upload callback fails to be returned.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
Examples
The following sample code provides an example on how to configure an upload callback:
// Construct an upload request.
// Specify the name of the bucket, the full path of the object, and the full path of the local file. In this example, the name of the bucket is examplebucket, the full path of the object is exampledir/exampleobject.txt, and the full path of the local file is /storage/emulated/0/oss/examplefile.txt.
// Do not include the bucket name in the full path of the object.
PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");
put.setCallbackParam(new HashMap<String, String>() {
{
put("callbackUrl", "http://oss-demo.aliyuncs.com:23450");
put("callbackHost", "yourCallbackHost");
put("callbackBodyType", "application/json");
put("callbackBody", "{\"mimeType\":${mimeType},\"size\":${size}}");
}
});
OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, PutObjectResult result) {
Log.d("PutObject", "UploadSuccess");
// Obtain the returned callback information. You can obtain the callback information only if you specify servercallback in the request.
String serverCallbackReturnJson = result.getServerCallbackReturnBody();
Log.d("servercallback", serverCallbackReturnJson);
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Handle exceptions.
}
});
The following sample code provides an example on how to configure custom parameters:
put.setCallbackParam(new HashMap<String, String>() {
{
put("callbackUrl", "http://oss-demo.aliyuncs.com:23450");
put("callbackHost", "yourCallbackHost");
put("callbackBodyType", "application/json");
put("callbackBody", "{\"object\":${object},\"size\":${size},\"my_var1\":${x:var1},\"my_var2\":${x:var2}}");
}
});
put.setCallbackVars(new HashMap<String, String>() {
{
put("x:var1", "value1");
put("x:var2", "value2");
}
});
References
For the complete sample code that is used to configure upload callbacks, visit GitHub.
For more information about the API operation that you can call to configure upload callbacks, see Callback.
For more information about how to initialize an OSSClient instance, see Initialization.