Feature Overview
This document describes how to limit the speed on a single URL when calling the upload or download API.
SDK API References
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 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format ofBucketName-APPID(APPIDis 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 keyString 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 nullString 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/sputObjectRequest.setTrafficLimit(1024 * 1024 * 8);// Upload the fileCOSXMLUploadTask cosxmlUploadTask = transferManager.upload(putObjectRequest, uploadId);// Set the upload progress callbackcosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic 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 progresscosxmlUploadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
Note
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, aHEADrequest 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 haveHeadObjectpermission.// InitializeTransferConfig. 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 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format ofBucketName-APPID(APPIDis 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 pathString savePathDir = context.getExternalCacheDir().toString();// Name of the locally saved file; if this is null, the COS filename will be usedString 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/sgetObjectRequest.setTrafficLimit(1024 * 1024 * 8);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);// Set the download progress callbackcosxmlDownloadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});// Set the response callbackcosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic 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 progresscosxmlDownloadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
Note