Todos os produtos
Search
Central de documentação

Object Storage Service:Upload callback (Browser.js SDK)

Última atualização: Jul 03, 2026

Inclua os parâmetros de callback na solicitação de upload para que o OSS envie uma notificação ao servidor da aplicação após o envio de um arquivo (Object).

Nota

Configure definições de callback, como callbackSNI. Para mais informações, consulte Callback.

Observações de uso

  • Ao usar ferramentas de empacotamento como Webpack e Browserify, instale o OSS SDK for Browser.js com o comando npm install ali-oss.

  • O navegador rejeita solicitações a buckets do OSS sem regras CORS configuradas. Portanto, configure as regras CORS no bucket antes de acessá-lo pelo navegador. Para mais detalhes, consulte Installation.

  • O OSS SDK for Browser.js executa principalmente em navegadores. Para evitar a exposição do par de AccessKey, use credenciais de acesso temporárias obtidas pelo Security Token Service (STS) para acessar o OSS.

    Essas credenciais temporárias incluem um par de AccessKey e um token de segurança. O par de AccessKey contém um AccessKeyID e um AccessKeySecret. Para obter credenciais de acesso temporárias, consulte Use STS for temporary access authorization.

Código de exemplo

O código a seguir demonstra como implementar um callback de upload:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>

  <body>
    <button id="submit">Upload Callback</button>
    <!--Import the SDK file-->
    <script
      type="text/javascript"
      src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"
    ></script>
    <script type="text/javascript">
      const client = new OSS({
        // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
        region: "yourRegion",
        authorizationV4: true,
        // The temporary AccessKey pair (AccessKey ID and AccessKey secret) obtained from the Security Token Service (STS).
        accessKeyId: "yourAccessKeyId",
        accessKeySecret: "yourAccessKeySecret",
        // The security token (SecurityToken) obtained from the STS service.
        stsToken: "yourSecurityToken",
        // Specify the bucket name. For example, examplebucket.
        bucket: "examplebucket",
      });

      const options = {
        callback: {
          // The server address for the callback request. The address must be an Internet URL.
          url: "http://examplebucket.aliyuncs.com:23450",
          // The value of Host in the callback request header. This is the Host value configured on your server.
          // host: 'yourHost',
          // The value of the request body for the callback.
          body: "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&var1=${x:var1}&var2=${x:var2}",
          // The Content-Type of the callback request.
          // contentType: 'application/x-www-form-urlencoded',
          // Specifies whether OSS sends a Server Name Indication (SNI) to the origin URL specified by callbackUrl when the client initiates a callback request.
          callbackSNI: true,
          // The custom parameters for the callback request.
          customValue: {
            var1: "value1",
            var2: "value2",
          },
        },
      };
      // Get the DOM.
      const submit = document.getElementById("submit");
      // Upload callback.
      submit.addEventListener("click", () => {
        client
          .put("example.txt", new Blob(["Hello World"]), options)
          .then((r) => console.log(r));
      });
    </script>
  </body>
</html>

Referências

Para detalhes sobre a operação de API de callback de upload, consulte Callback.