After an object is uploaded, Object Storage Service (OSS) can send an HTTP POST request to your application server. Use upload callbacks to trigger server-side processing — such as updating a database record or starting a media transcoding job — immediately after a successful upload.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid credentialsA running callback server that accepts HTTP POST requests
Usage notes
Examples in this topic use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.
This topic creates the OSSClient instance using an OSS endpoint. To create an OSSClient using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
How it works
Configure a callback when submitting the upload request. Specify the callback URL, the callback body, and any custom variables.
OSS uploads the object. On success, OSS sends an HTTP POST request to your callback URL with the body you specified.
A failed callback does not affect the status of a successful file upload.
Configure an upload callback
The following example configures an upload callback for a PutObject request.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Set the endpoint of the region where your bucket is located.
For the China (Hangzhou) region: https://oss-cn-hangzhou.aliyuncs.com */
std::string Endpoint = "yourEndpoint";
/* Set the region ID. For the China (Hangzhou) region: cn-hangzhou */
std::string Region = "yourRegion";
/* Bucket name. Example: examplebucket */
std::string BucketName = "examplebucket";
/* Full path of the object, excluding the bucket name.
Example: exampledir/exampleobject.txt */
std::string ObjectName = "exampledir/exampleobject.txt";
/* URL of your callback server */
std::string ServerName = "https://example.aliyundoc.com:23450";
/* Initialize network resources */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Read access credentials from environment variables.
Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
*content << "Thank you for using Alibaba Cloud Object Storage Service!";
/* Build the callback configuration */
std::string callbackBody = "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&my_var1=${x:var1}";
ObjectCallbackBuilder builder(ServerName, callbackBody, "", ObjectCallbackBuilder::Type::URL);
std::string value = builder.build();
/* Add a custom variable */
ObjectCallbackVariableBuilder varBuilder;
varBuilder.addCallbackVariable("x:var1", "value1");
std::string varValue = varBuilder.build();
/* Attach the callback config to the upload request */
PutObjectRequest request(BucketName, ObjectName, content);
request.MetaData().addHeader("x-oss-callback", value);
request.MetaData().addHeader("x-oss-callback-var", varValue);
auto outcome = client.PutObject(request);
/* Release network resources */
ShutdownSdk();
return 0;
}Callback parameters
| Parameter | Description |
|---|---|
ServerName | The callback URL. OSS sends an HTTP POST request to this address after the upload succeeds. |
callbackBody | The body of the callback request. Supports OSS system variables (such as ${bucket} and ${object}) and custom variables (such as ${x:var1}). For the example above, the expanded body sent to your server is: bucket=examplebucket&object=exampledir/exampleobject.txt&etag=<etag>&size=<size>&mimeType=<mimeType>&my_var1=value1. |
ObjectCallbackBuilder::Type::URL | Encoding format of the callback body. URL encodes the body as application/x-www-form-urlencoded. |
x-oss-callback | Request header that carries the Base64-encoded callback configuration built by ObjectCallbackBuilder.build(). |
x-oss-callback-var | Request header that carries the Base64-encoded custom variables built by ObjectCallbackVariableBuilder.build(). |
System variables in the callback body
Use these variables in callbackBody to include object metadata in the callback request sent to your server.
| Variable | Type | Description |
|---|---|---|
${bucket} | String | The name of the bucket. |
${object} | String | The full path of the uploaded object. |
${etag} | String | The ETag of the uploaded object. |
${size} | String | The size of the uploaded object in bytes. |
${mimeType} | String | The MIME type of the uploaded object. |
${x:<key>} | String | A custom variable. The key must start with x:. Set the value using addCallbackVariable. |
What's next
Callback — API reference for upload callbacks