This topic describes how to use OSS C SDK to complete routine operations such as bucket creation, objects uploads, and object downloads.
Demo
- Linux demo
Linux-based demo: aliyun-oss-c-sdk-demo.tar.gz.
- Download and extract the demo project.
- You must specify the installation directory for oss-c-sdk-demo-specified-installation: /home/your/oss/csdk. You do not need to specify installation directories for other demo projects because they are automatically installed based on the OSS C SDK and other dependent third-party libraries.
- Replace the OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, and BUCKET_NAME in the
demo with valid values. Use
LD_LIBRARY_PATH
to specify the directory of the OSS C SDK and the dynamic libraries of the dependent libraries if they are not in the system directory. - Enter the directory of the project (oss-c-sdk-demo-xxx), run the
make
command to compile the demo project. Then, run the./main
command to run the executable program. If the project needs to be re-compiled, run themake clean
command first before compiling.
- Windows demo
Windows-based demo: aliyun-oss-c-sdk-sample.zip. You can download more demo projects from GitHub.
- Download and extract the demo project.
- Use Visual Studio to open the demo project, and then replace the following parameters in the oss_config.c with valid values: OSS_ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, BUCKET_NAME, OBJECT_NAME, MULTIPART_UPLOAD_FILE_PATH, and DIR_NAME. Compile and run the project. You can develop your own project based on the demo project.
Create a bucket
A bucket is a global namespace on OSS used to store objects. It is equivalent to a data container. You can run the following code to create a bucket:
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize aos_string_t. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Determine whether the CNAME is used. 0 indicates the CNAME is not used. */
options->config->is_cname = 0;
/* Used to configure network parameters, such as timeout */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call the aos_http_io_initialize method in main() to initialize global resources, such as the network and memory. */
if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
exit(1);
}
/* Initialize a memory pool used to manage memories, which is equivalent to apr_pool_t. The implementation code is included in the apr library. */
aos_pool_t *pool;
/* Re-create a new memory pool. The second parameter is NULL, indicating that it does not inherit from any other memory pools. */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter mainly includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options;
/* Allocate memories in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Use oss_client_options to initialize client options */
init_options(oss_client_options);
/* Create the bucket. */
aos_status_t *resp_status;
aos_table_t *resp_headers;
oss_acl_e oss_acl = OSS_ACL_PRIVATE;
aos_string_t bucket;
/* Assign a char* to the bucket. */
aos_str_set(&bucket, bucket_name);
resp_status = oss_create_bucket(oss_client_options, &bucket, oss_acl, &resp_headers);
/* Identify whether the request succeeds */
if (aos_status_is_ok(resp_status)) {
printf("create bucket succeeded\n");
} else {
printf("create bucket failed\n");
}
/* Release the memory pool, that is, memories allocated to resources during the request. */
aos_pool_destroy(pool);
/* Release allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
For more information about endpoints, see Regions and endpoints.
Upload an object
You can run the following code to upload an object to OSS:
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
const char *object_name = "<yourObjectName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize aos_string_t. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Determine whether the CNAME is used. 0 indicates the CNAME is not used. */
options->config->is_cname = 0;
/* Used to configure network parameters, such as timeout */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) { = AOSE_OK) {
exit(1);
}
/* Initialize a memory pool used to manage memories, which is equivalent to apr_pool_t. The implementation code is included in the apr library. */
aos_pool_t *pool;
/* Re-create a new memory pool. The second parameter is NULL, indicating that it does not inherit from any other memory pool. */ */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter mainly includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options;
/* Allocate memory in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Use oss_client_options to initialize client options */
init_options(oss_client_options);
/* Initialization parameters */
aos_string_t bucket;
aos_string_t object;
aos_status_t *resp_status;
aos_table_t *headers;
aos_table_t *resp_headers;
aos_list_t buffer;
aos_buf_t *content = NULL;
const char *data = "More than just cloud.";
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
headers = aos_table_make(pool, 0);
/* Convert the char* type data to the aos_list_t type data. */
aos_list_init(&buffer);
content = aos_buf_pack(oss_client_options->pool, data, strlen(data));
aos_list_add_tail(&content->node, &buffer);
/* Upload an object. */
resp_status = oss_put_object_from_buffer(oss_client_options, &bucket, &object, &buffer, headers, &resp_headers);
/* Determine whether the upload request is successful. */
if (aos_status_is_ok(resp_status)) {
printf("put file succeeded\n");
} else {
printf("put file failed\n");
}
/* Release the memory pool, that is, memories allocated to resources during the request. */
aos_pool_destroy(pool);
/* Release allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
Download an object
You can run the following code to download a specified object:
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
const char *object_name = "<yourObjectName>";
const char *download_filename = "<yourDownloadedFilename>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize aos_string_t. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Determine whether the CNAME is used. 0 indicates the CNAME is not used. */
options->config->is_cname = 0;
/* Used to configure network parameters, such as timeout */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
exit(1);
}
/* Initialize a memory pool used to manage memories, which is equivalent to apr_pool_t. The implementation code is included in the apr library. */
aos_pool_t *pool;
/* Re-create a new memory pool. The second parameter is NULL, indicating that it does not inherit from any other memory pools. */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter mainly includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options;
/* Allocate memories in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Use oss_client_options to initialize client options */
init_options(oss_client_options);
/* Initialization parameters */
aos_string_t bucket;
aos_string_t object;
aos_string_t file;
aos_status_t *resp_status;
aos_table_t *headers = NULL;
aos_table_t *params = NULL;
aos_table_t *resp_headers = NULL;
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
aos_str_set(&file, download_filename);
headers = aos_table_make(pool, 0);
/* Download an object. */
resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
/* Determine whether the download request is successful. */
if (aos_status_is_ok(resp_status)) {
printf("get object to local file succeeded\n");
} else {
printf("get object to local file failed\n");
}
/* Release the memory pool, that is, memories allocated to resources during the request. */
aos_pool_destroy(pool);
/* Release allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
List objects in a bucket
You can run the following code to list objects stored in a specified bucket.
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize aos_string_t. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Determine whether the CNAME is used. 0 indicates the CNAME is not used. */
options->config->is_cname = 0;
/* Used to configure network parameters, such as timeout */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
exit(1);
}
/* Initialize a memory pool used to manage memories, which is equivalent to apr_pool_t. The implementation code is included in the apr library. */
aos_pool_t *pool = NULL;
/* Re-create a new memory pool. The second parameter is NULL, indicating that it does not inherit from any other memory pool. */ */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter mainly includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options = NULL;
/* Allocate memory in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Use oss_client_options to initialize client options */
init_options(oss_client_options);
/* Initialization parameters */
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
/* List objects */
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
/* Determine whether the list request is successful. */
if (! aos_status_is_ok(resp_status))
{
printf("list object failed\n");
}
else{
/* Print objects. */
printf("Object\tSize\tLastModified\n");
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
printf("Total %d\n", size);
}
/* Release the memory pool, that is, memories allocated to resources during the request. */
aos_pool_destroy(pool);
/* Release allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
Delete an object.
You can run the following code to delete an object:
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
const char *object_name = "<yourObjectName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize aos_string_t. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Determine whether the CNAME is used. 0 indicates the CNAME is not used. */
options->config->is_cname = 0;
/* Used to configure network parameters, such as timeout */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
exit(1);
}
/* Initialize a memory pool used to manage memories, which is equivalent to apr_pool_t. The implementation code is included in the apr library. */
aos_pool_t *pool = NULL;
/* Re-create a new memory pool. The second parameter is NULL, indicating that it does not inherit from any other memory pool. */ */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter mainly includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options = NULL;
/* Allocate memory in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Use oss_client_options to initialize client options */
init_options(oss_client_options);
/* Initialization parameters */
aos_string_t bucket;
aos_string_t object;
aos_status_t *resp_status = NULL;
aos_table_t *resp_headers = NULL;
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
/* Delete an object. */
resp_status = oss_delete_object(oss_client_options, &bucket, &object, &resp_headers);
/* Determine whether the delete request is successful. */
if (aos_status_is_ok(resp_status)) {
printf("delete object succeeded\n");
} else {
printf("delete object failed\n");
}
/* Release the memory pool, that is, memories allocated to resources during the request. */
aos_pool_destroy(pool);
/* Release allocated global resources. */
aos_http_io_deinitialize();
return 0;
}