All Products
Search
Document Center

Object Storage Service:Upload callbacks

Last Updated:Mar 19, 2026

When an object is uploaded, OSS sends an HTTP POST request to your application server. To trigger this callback, add callback parameters to the upload request.

Prerequisites

Before you begin, ensure that you have:

  • Reviewed the upload callback feature. For more information, see Upload callback

Configure an upload callback

The following example uploads a local file (examplefile.txt) to a bucket named examplebucket. After the upload, OSS sends a callback request to the specified server. The uploaded object is stored as exampleobject.txt.

const OSS = require("ali-oss");

var path = require("path");

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: "yourregion",
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket.
  bucket: "examplebucket",
});

const options = {
  callback: {
    // Specify the address of the callback server that receives the callback request. Example: http://oss-demo.aliyuncs.com:23450.
    url: "http://oss-demo.aliyuncs.com:23450",
    // (Optional) Specify the Host field included in the callback request header.
    //host: 'yourCallbackHost',
    // Specify the body of the callback request.
    body: "bucket=${bucket}&object=${object}&var1=${x:var1}&var2=${x:var2}",
    // Specify Content-Type in the callback request.
    contentType: "application/x-www-form-urlencoded",
    // Specifies whether OSS sends Server Name Indication (SNI) to the origin address specified by callbackUrl when a callback request is initiated from the client.
    callbackSNI: true,
    // Configure custom parameters for the callback request.
    customValue: {
      var1: "value1",
      var2: "value2",
    },
  },
};

async function put() {
  try {
    // Specify the object path and the local file path. Do not include the bucket name in the object path.
    // If you do not specify the local file path, the file is uploaded from the local path of the project.
    let result = await client.put(
      "exampleobject.txt",
      path.normalize("/localpath/examplefile.txt"),
      options
    );
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();

Callback parameters

ParameterDescription
urlThe callback server URL. OSS sends the callback request to this address.
bodyThe 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=exampleobject.txt&var1=value1&var2=value2.
contentTypeContent-Type of the callback request body. Example: application/x-www-form-urlencoded.
host(Optional) The value of the Host header in the callback request.
callbackSNI(Optional) Whether OSS sends Server Name Indication (SNI) to the address specified by callbackUrl. Set to true if your callback server requires SNI. Whether OSS sends callbackSNI depends on how the callback parameters are constructed. For more information, see Callback.
customValue(Optional) Custom variables to include in the callback request body. Reference each key in body using the format ${x:key} (for example, ${x:var1}).

Next steps

  • For the complete sample code, see GitHub.

  • For API parameter details, see Callback.

  • For callback error causes and solutions, see HTTP status code 203.

  • For parameter settings and usage notes for the callback feature, see Callback.