Help Center/ Object Storage Service/ SDK Reference/ Android/ FAQ/ How Do I Implement Upload and Download Bandwidth Throttling?
Updated on 2026-09-20 GMT+08:00

How Do I Implement Upload and Download Bandwidth Throttling?

OBS allows you to configure single-connection bandwidth throttling during object upload or download. You can set the custom header x-obs-traffic-limit in a request. The SDK transparently transmits this header to the server, which verifies it and applies bandwidth throttling accordingly.

  • Throttling unit: bit/s. For example, 819200 indicates a limit of 100 KB/s. The calculation formula is as follows: 819,200 (bit/s)/8 = 102,400 (byte/s) = 100 (KB/s).
  • Application scenario: You can configure single-connection bandwidth throttling to control the transmission rate of OBS requests in an environment with limited bandwidth to prevent excessive network resource consumption.
  • Applicable APIs: PutObject (uploading an object) and GetObject (downloading an object)
Table 1 Constraints

Item

Description

Throttling header

x-obs-traffic-limit

Throttling value range

819200 to 838860800 (100 KB/s to 100 MB/s)

Throttling unit

bit/s

Setting method

Using PutObjectRequest.setUserHeaders() or GetObjectRequest.setUserHeaders() to set a custom header

Sample Code 1: Limiting Upload Bandwidth

The following code shows how to configure single-connection bandwidth throttling during object upload.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
// Enter the endpoint corresponding to the bucket. CN North-Beijing4 is used here as an example. Replace it with the one currently in use.
String endPoint = "https://your-endpoint";

// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

try {
    // Upload a file.
    // localfile indicates the path of the local file to be uploaded, which must include the file name.
    PutObjectRequest request = new PutObjectRequest();
    request.setBucketName("examplebucket");
    request.setObjectKey("objectname");
    request.setFile(new File("localfile"));

    HashMap<String, String> userHeaders = new HashMap<>();
    // x-obs-traffic-limit is the custom throttling header. The unit is bit/s. 819200 (100KB/s) is used here as an example.
    userHeaders.put("x-obs-traffic-limit", "819200");
    request.setUserHeaders(userHeaders);

    PutObjectResult result = obsClient.putObject(request);
    Log.i("PutObject", "putObject successfully");
} catch (ObsException e) {
    Log.e("PutObject", "Response Code: " + e.getResponseCode());
    Log.e("PutObject", "Error Message: " + e.getErrorMessage());
    Log.e("PutObject", "Error Code:       " + e.getErrorCode());
    Log.e("PutObject", "Request ID:      " + e.getErrorRequestId());
    Log.e("PutObject", "Host ID:           " + e.getErrorHostId());
} catch (Exception e) {
    Log.e("PutObject", "putObject failed: " + e.getMessage());
}

Sample Code 2: Limiting the Download Bandwidth

The following code shows how to configure single-connection bandwidth throttling during object download.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
// Enter the endpoint corresponding to the bucket. CN North-Beijing4 is used here as an example. Replace it with the one currently in use.
String endPoint = "https://your-endpoint";

// Create an ObsClient instance.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

try {
    // Download an object.
    GetObjectRequest request = new GetObjectRequest("examplebucket", "objectname");

    HashMap<String, String> userHeaders = new HashMap<>();
    // x-obs-traffic-limit is the custom throttling header. The unit is bit/s. 819200 (100KB/s) is used here as an example.
    userHeaders.put("x-obs-traffic-limit", "819200");
    request.setUserHeaders(userHeaders);

    ObsObject obsObject = obsClient.getObject(request);
    // Read the object content.
    InputStream input = obsObject.getObjectContent();
    byte[] b = new byte[1024];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;
    while ((len = input.read(b)) != -1) {
        bos.write(b, 0, len);
    }
    Log.i("GetObject", "getObjectContent successfully");
    Log.i("GetObject", new String(bos.toByteArray()));
    bos.close();
    input.close();
} catch (ObsException e) {
    Log.e("GetObject", "Response Code: " + e.getResponseCode());
    Log.e("GetObject", "Error Message: " + e.getErrorMessage());
    Log.e("GetObject", "Error Code:       " + e.getErrorCode());
    Log.e("GetObject", "Request ID:      " + e.getErrorRequestId());
    Log.e("GetObject", "Host ID:           " + e.getErrorHostId());
} catch (Exception e) {
    Log.e("GetObject", "getObjectContent failed: " + e.getMessage());
}