When you access objects in Object Storage Service (OSS) by using a client, especially a client on which traffic throttling is difficult to implement, a large amount of bandwidth of OSS may be occupied, which obstructs other applications from accessing OSS. To prevent this issue, you can use the single-connection bandwidth throttling feature that is provided by OSS to throttle the bandwidth for object downloads. This way, sufficient bandwidth can be reserved for other applications to access OSS.

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.
  • 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.

    For more information about how to use STS, see Use temporary credentials provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain temporary access credentials. Temporary access credentials include a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Sample code

The following code provides an example on how to configure bandwidth throttling when you download an object by using a signed URL:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id="dowload">Download</button>
  <!-- import an 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 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',
         // Specify the temporary AccessKey pair obtained from STS. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
         accessKeyId: 'yourAccessKeyId',
         accessKeySecret: 'yourAccessKeySecret',
         // Specify the security token that you obtained from STS. 
        stsToken: 'yourSecurityToken',
        // Specify the name of the bucket. Example: examplebucket. 
        bucket: "examplebucket",
      });

    const download = document.getElementById("dowload");

    download.addEventListener("click", () => {
      console.log(client.signatureUrl('bigData', {
       // Set the download speed limit to 100 KB/s. 
        trafficLimit: 1024 * 8 * 100, 
        responsel: {
          'content-type': 'attachment'
        }
      }))
    });
  </script>
</body>

</html>

References

For more information about the complete sample code of single-connection bandwidth throttling, visit GitHub.