Limiting Download Speed (SDK for C)
If you have any questions during development, post them on the Issues page of GitHub.
Function
Limiting download speed refers to limiting the bandwidth of a single connection request during object download. This prevents a single download request from occupying too much bandwidth and affecting other services. The OBS SDK for C uses the download_limit parameter in a download request to limit the download speed. The SDK writes the limit to the x-obs-traffic-limit request header, and the server controls the download speed.
The following API supports download speed limiting: Downloading an Object (get_object).
Restrictions
- To limit the download speed, you must be the bucket owner or have the object download permission (obs:object:GetObject granted using IAM or GetObject granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
- Objects in the Archive storage class can be downloaded only when they are in the Restored status.
Method
void get_object(const obs_options *options, obs_object_info *object_info,
obs_get_conditions *get_conditions,
server_side_encryption_params *encryption_params,
obs_get_object_handler *handler, void *callback_data); Request Parameters
The parameters related to download speed limiting are in the obs_get_conditions structure. You can use init_get_properties to initialize the structure.
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| download_limit | uint64_t | No | Explanation: Bandwidth limit for single connection requests. Restrictions: None Value range: 819200 to 838860800, in bit/s. Default value: 0: The bandwidth is not limited. |
Sample Code
#include "eSDKOBS.h"
#include <stdio.h>
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
obs_status get_object_data_callback(int buffer_size, const char *buffer, void *callback_data);
int main()
{
obs_initialize(OBS_INIT_ALL);
obs_options options;
init_obs_options(&options);
// Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
// 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 for identity authentication. Before running the code in this example, configure local environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
options.bucket_options.bucket_name = "example-bucket-name";
// Set download conditions, including the speed limit.
obs_get_conditions get_conditions;
init_get_properties(&get_conditions);
get_conditions.download_limit = 819200; // Limit the download speed to 100 KB/s (819200 bit/s).
obs_object_info object_info = {0};
object_info.key = "objectname";
obs_get_object_handler handler = {
&response_properties_callback,
&response_complete_callback,
&get_object_data_callback
};
obs_status ret_status = OBS_STATUS_BUTT;
get_object(&options, &object_info, &get_conditions, NULL, &handler, &ret_status);
if (OBS_STATUS_OK == ret_status) {
printf("get object with download limit successfully.\n");
} else {
printf("get object with download limit failed(%s).\n", obs_get_status_name(ret_status));
}
obs_deinitialize();
}
obs_status get_object_data_callback(int buffer_size, const char *buffer, void *callback_data)
{
(void)buffer_size; (void)buffer; (void)callback_data;
return OBS_STATUS_OK;
}
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
(void)properties; (void)callback_data;
return OBS_STATUS_OK;
}
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
if (callback_data) { *(obs_status*)callback_data = status; }
if (error && error->message) { printf("Error: %s\n", error->message); }
}
- You can perform operations on the input streams of an object to read and write the object contents to a local file or to the memory.
- getcondition can be left empty, indicating that the entire object is downloaded.
Helpful Links
- For details about the API for limiting the download speed, see Downloading an Object.
- OBS Error Codes
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot