All Products
Search
Document Center

Object Storage Service:Single-connection bandwidth throttling

Last Updated:Nov 10, 2024

This topic describes how to add parameters in an object upload or download request to set the limit of upload or download bandwidth. This ensures sufficient bandwidth for other applications.

Configure bandwidth throttling for simple uploads and downloads

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

const OSS = require('ali-oss')

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 you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of your bucket. 
  bucket: 'yourbucketname'
});

// Configure the speed limit by using the request header. 
const headers = {  
 // Set the minimum bandwidth to 100 KB/s. 
 'x-oss-traffic-limit': 8 * 1024 * 100 
}

// Configure bandwidth throttling for the object to be uploaded. 
async function put() {
  // Specify the path of the object. 
  const filePath = 'D:\\localpath\\examplefile.txt'; 
  // Create a file stream for the object. 
  const fileStream = fs.createReadStream(filePath); 
  const result = await client.putStream('file-name', fileStream, {
    // Set the request header properly. 
    headers, 
    // Set the default timeout period to 60000. Unit: milliseconds. If the duration of an object upload exceeds the timeout period, an exception is thrown. When you configure bandwidth throttling for object uploads, modify the timeout period. 
    timeout: 60000 
  });
  console.log(result);
}

put()

// Configure bandwidth throttling for the object to be downloaded. 
async function get() {
  const result = await client.get('file name', {
    headers,
    // Set the default timeout period to 60000. Unit: milliseconds. If the duration of an object download exceeds the timeout period, an exception is thrown. When you configure bandwidth throttling for object downloads, modify the timeout period. 
    timeout: 60000 
  })
  console.log(result)
}

get()

Configure bandwidth throttling when you use signed URLs to upload or download objects

The following code provides an example on how to configure single-connection bandwidth throttling when you use signed URLs to upload or download objects:

const OSS = require('ali-oss')

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 you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of your bucket. 
  bucket: 'yourbucketname',
});

// Configure bandwidth throttling for the object to be uploaded by using URL Query. 
async function putByQuery() {
  const url = client.signatureUrl('file name', {
    // Set the minimum bandwidth to 100 KB/s. 
    trafficLimit: 8 * 1024 * 100, 
    // Configure the PUT request. 
    method: 'PUT' 
  })
  // Specify the path of the object. 
  const filePath = 'D:\\localpath\\examplefile.txt'; 
  // Create a file stream for the object. 
  const fileStream = fs.createReadStream(filePath); 
  const result = await client.urllib.request(url, {
    method: 'PUT',
    // Specify the file stream as a parameter. 
    stream: fileStream, 
    // Set the default timeout period to 60000. Unit: milliseconds. We recommend that you modify the timeout period after you configure bandwidth throttling. Otherwise, the request fails. 
    timeout: 60000, 
  });

  console.log(result)
}

putByQuery()

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

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

  console.log(result)
}

getByQuery()

References

For the complete sample code for single-connection bandwidth throttling, visit GitHub.