This topic describes how to ensure other applications have sufficient bandwidth when you perform upload and download operations on OSS. You can include the x-oss-traffic-limit parameter in your requests and set bandwidth limits.

Configure bandwidth throttling for simple uploads and downloads

The following code provides an example on how to configure bandwidth throttling for simple uploads and downloads of objects:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Configure bandwidth throttling for the object to be uploaded.
async function put() {
  const result = await client.putStream('<file name>', '<file stream>', {
    headers,
    timeout: 60000 // Set the default timeout duration to 60000 ms. When the speed of uploading objects exceeds 60000 ms, the error message comes up. We recommend that you modify the timeout duration.
  })
  console.log(result)
}
put()

// Configure bandwidth throttling for the object to be downloaded.
async function get() {
  const result = await client.get('<file name>', {
    headers,
    timeout: 60000 // Set the default timeout duration to 60000 ms. When the speed of downloading objects exceeds 60000 ms, the error message comes up. We recommend that you modify the timeout duration.
  })
  console.log(result)
}
get()

Configure bandwidth throttling for uploads and downloads that use signed URLs

The following code provides an example on how to configure single-connection bandwidth throttling when you upload and download objects from and to OSS by using signed URLs:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Configure bandwidth throttling for the object to be uploaded by using url query.
async function putByQuery() {
  const url = client.signatureUrl('<file name>', {
    trafficLimit: 8 * 1024 * 100, // Set the minimum bandwidth to 100 KB/s.
    method: 'PUT' // Configure PUT request method.
  })

  const result = await client.urllib.request(url, {
    method: 'PUT',
    stream: '<file stream>'
    timeout: 60000, // Set the default timeout duration to 60000 ms. We recommend that you modify the timeout duration after you configure bandwidth throttling. Otherwise, the request fails.
  });

  console.log(result)
}
putByQuery()

// Configure bandwidth throttling for the object to be uploaded by using url query.
async function getByQuery() {
  const url = client.signatureUrl('<file name>', {
    trafficLimit: 8 * 1024 * 100, // Set the minimum bandwidth to 100 KB/s.
  })

  const result = await client.urllib.request(url, {
    timeout: 60000, // Set the default timeout duration to 60000 ms. We recommend that you modify the timeout duration after you configure bandwidth throttling. Otherwise, the request fails.
  });

  console.log(result)
}
getByQuery()