Single-connection bandwidth limit

Last updated: 2023-09-13 11:07:09

Feature Overview

This document describes how to limit the speed on a single URL when calling the upload or download API.

SDK API References

For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.

Notes

The speed limit range is set between 819,200 - 838,860,800 bits/s, which is equivalent to 800 Kb/s - 800 Mb/s. If the value exceeds this range, a 400 error will be returned.
Note
For more information about single URL speed limiting, see the Single URL Speed Limiting Developer Guide.

Sample 1. Limiting single-URL speed on uploads

TransferConfig transferConfig = new TransferConfig.Builder().build();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXmlService,
transferConfig);

// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
String srcPath = new File(context.getCacheDir(), "exampleobject")
.toString(); // The absolute path of the local file
// If there is an uploadId for the initialized multipart upload, assign the value of the uploadId here to resume the upload; otherwise, assign null
String uploadId = null;

PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);
// Set the bandwidth limit for a single URL in bit/s. In the example, the limit is set to 1 Mbit/s
putObjectRequest.setTrafficLimit(1024 * 1024 * 8);

// Upload the file
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(putObjectRequest, uploadId);

// Set the upload progress callback
cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long complete, long target) {
// todo Do something to update progress...
}
});
// Set the response callback
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you are using Kotlin to call the API, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called, i.e.:
// Is the type of clientException CosXmlClientException? Is the type of serviceException CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
// Set the job status callback where you can view the job progress
cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
// todo notify transfer state
}
});
Note
For the complete sample, go to GitHub.

Sample 2. Limiting single-URL speed on downloads

//.cssg-snippet-body-start:[transfer-download-object]
// The advanced download API supports checkpoint restart. To do so, a HEAD request will be sent first to get file information before download.
// If you are using a temporary key or accessing with a sub-account, please make sure that you have HeadObject permission.

// Initialize TransferConfig. The default configuration is used here. If you need to customize it, please see the SDK API documentation.
TransferConfig transferConfig = new TransferConfig.Builder().build();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXmlService,
transferConfig);

// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
// Local directory path
String savePathDir = context.getExternalCacheDir().toString();
// Name of the locally saved file; if this is null, the COS filename will be used
String savedFileName = "exampleobject";

GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);
// Set the bandwidth limit for a single URL in bit/s. In the example, the limit is set to 1 Mbit/s
getObjectRequest.setTrafficLimit(1024 * 1024 * 8);

Context applicationContext = context.getApplicationContext(); // application
// context
COSXMLDownloadTask cosxmlDownloadTask =
transferManager.download(applicationContext, getObjectRequest);

// Set the download progress callback
cosxmlDownloadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long complete, long target) {
// todo Do something to update progress...
}
});
// Set the response callback
cosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLDownloadTask.COSXMLDownloadTaskResult downloadTaskResult =
(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;
}

// If you are using Kotlin to call the API, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called, i.e.:
// Is the type of clientException CosXmlClientException? Is the type of serviceException CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
// Set the job status callback where you can view the job progress
cosxmlDownloadTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
// todo notify transfer state
}
});
Note
For the complete sample, go to GitHub.