Initialization

Last Updated: Aug 09, 2017

Determine an endpoint

An endpoint is the address of Alibaba Cloud OSS in a region. It currently supports two formats.

Endpoint type Description
OSS region address Address of the region where an OSS bucket is located. For details about the endpoints in various regions, see here.
User-defined domain name Domain name defined by the user, with the CNAME directing to the OSS domain.

OSS region address

You can use one of the following two methods to search for an endpoint mapped to the address of the region where an OSS bucket is located:

  • Query the mapping relationship between the endpoint and the region. For details, click to view details.
  • Log on to the OSS Console, open the Bucket Overview page, and find the suffix of the bucket domain. For example, the suffix oss-cn-hangzhou.aliyuncs.com of the bucket domain bucket-1.oss-cn-hangzhou.aliyuncs.com is the endpoint of the bucket on the internet.

CNAME

You can bind your domain to a bucket through the CNAME and access the objects in the bucket through the domain.

For example, if you need to bind the new-image.xxxxx.com domain to the bucket named image in Shenzhen, do the following:

Contact your domain hosting provider for xxxxx.com to configure a new domain name resolution record used to resolve http://new-image.xxxxx.com to http://image.oss-cn-shenzhen.aliyuncs.com. The record type is CNAME.

Configure an access key

To access Alibaba Cloud OSS, you need a valid pair of access keys (an AccesskeyID and an AccessKeySecret) for signature authentication. The access keys can be obtained via the steps below:

After obtaining the AccessKeyId and AccessKeySecret, you can follow the steps below for initialization.

Initialize request options

To use OSS C SDK, you need to initialize request options (oss_request_options_t). Config is used to store basic information for accessing OSS, such as the OSS domain name, and the user’s AccessKeyID and AccessKeySecret.

The is_cname variable specifies whether to use CNAME for accessing the OSS. The ctl variable sets the control information for the OSS access.

  1. void init_options(oss_request_options_t *options) {
  2. options->config = oss_config_create(options->pool);
  3. aos_str_set(&options->config->endpoint, "<Your endpoint>");
  4. aos_str_set(&options->config->access_key_id, "<Your AccessKeyId>");
  5. aos_str_set(&options->config->access_key_secret, "<Your AccessKeySecret>");
  6. options->config->is_cname = 0;
  7. options->ctl = aos_http_controller_create(options->pool, 0);
  8. }
  9. int main() {
  10. aos_pool_t *p;
  11. oss_request_options_t *options;
  12. /* Initialize the global variables. It should be placed at project startup and before all other logic */
  13. if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
  14. return -1;
  15. }
  16. /* Initialize the memory pool and options */
  17. aos_pool_create(&p, NULL);
  18. options = oss_request_options_create(p);
  19. init_options(options);
  20. /* Logic code */
  21. /* Release the memory pool resources including the memory allocated through the pool such as options */
  22. aos_pool_destroy(p);
  23. /* Release global resources. It should be placed before the project ending */
  24. aos_http_io_deinitialize();
  25. return 0;
  26. }

Notice:

  • If you want to use HTTPS, you only need to use https:// for the prefix when setting the endpoint.
  • Multithreading is supported, but the aos_http_io_initialize and aos_http_io_deinitialize methods only need to be called in the main thread. They are not required in other threads.
  • To set the OSS C SDK’s underlying libcurl communication parameters, you can set the ctl variable in the request options to control the minimum data upload speed, the connection timeout value, and the DNS cache expiration time.
  • You can also use the ctl variable to obtain the performance parameters of OSS access by OSS C SDK. Take data uploading as an example. After uploading the data, you can obtain a series of parameter indicators, such as the start time of data upload (start_time), the time of uploading the first byte (first_byte_time), and the time elapsed when data upload is finished (finish_time).
Thank you! We've received your feedback.