All Products
Search
Document Center

Object Storage Service:Single-connection bandwidth throttling (Browser.js SDK)

Last Updated:Nov 29, 2025

Clients that access files in OSS can consume a large amount of bandwidth. This can affect other applications, especially on clients where traffic shaping is difficult. To avoid this problem, you can use the single-connection bandwidth throttling feature in OSS. This feature lets you control traffic when you download files and ensures that sufficient network bandwidth is available for other applications.

Usage notes

  • When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.

  • If you want to access an OSS bucket from a browser but no CORS rules are configured for the bucket, the browser rejects the request. Therefore, you must configure CORS rules for a bucket if you want to access the bucket from a browser. For more information, see Installation.

  • In most cases, OSS SDK for Browser.js is used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use temporary access credentials obtained from Security Token Service (STS) to access OSS.

    The temporary access credentials consist of an AccessKey pair and a security token. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to obtain temporary access credentials, see Use STS for temporary access authorization.

Example code

The following code shows how to limit the download speed for a file using a signed URL:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>
  <body>
    <button id="download">Download</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({
    // Specify the bucket name. For example, examplebucket.
    bucket: 'examplebucket',
    // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
    region: 'yourRegion',
    authorizationV4: true,
    // The temporary AccessKey ID and AccessKey secret obtained from Security Token Service (STS).
    accessKeyId: 'yourAccessKeyId',
    accessKeySecret: 'yourAccessKeySecret',
    // The security token (SecurityToken) obtained from STS.
    stsToken: 'yourSecurityToken',
    // Set secure to true to use HTTPS. This prevents the browser from blocking the generated download link.
    secure: true,
  });

  document.getElementById("download").addEventListener("click", async (e) => {
    // Generate a signed URL.
    const signedUrl = await client.signatureUrlV4('GET', 3600, {
      queries:{
        // Limit the download speed to 100 KB/s.
        "x-oss-traffic-limit": 1024 * 8 * 100,
         // Specify the content-disposition header for the response from OSS.
        'response-content-disposition': 'attachment',
      }
    }, 'demo.pdf');

    // Open the link directly to download the file.
    window.location.href = signedUrl;
  });
    </script>
  </body>
</html>

References

For a complete code example of single-connection bandwidth throttling, see the GitHub example.