Obtaining Object Tags (SDK for C)
If you have any questions during development, post them on the Issues page of GitHub.
Function
This API is used to obtain the tags of an object.
Restrictions
- To obtain object tags, you must be the bucket owner or have the required permission (obs:object:GetObjectTagging granted using IAM or GetObjectTagging 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.
Method
void get_object_tagging(const obs_options *options, const char *key,
obs_get_object_tagging_handler *handler, void *callback_data); Request Parameters
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| options | const obs_options* | Yes | Explanation: Context of the requested bucket. You can set the AK, SK, endpoint, bucket, timeout interval, and temporary credentials through obs_options. Restrictions: None Value range: None Default value: None |
| key | char * | Yes | Explanation: Object name. An object name is a complete path of the object that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Restrictions: The name of each object in a bucket must be unique. Value range: The value can contain 1 to 1,024 characters. Default value: None |
| handler | Yes | Explanation: A callback structure where all members are pointers to callback functions, used to set the callback functions that handle response data. You can set a callback function to copy the response data from the server to callback_data (custom callback data). Restrictions: None Value range: None Default value: None | |
| callback_data | void * | No | Explanation: Custom callback data. Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| response_handler | obs_response_handler* | Yes | Explanation: Response callback function structure. Restrictions: None Value range: None Default value: None |
| get_object_tagging_callback | Yes | Explanation: The callback function pointer tag. The content of tagging_count and tagging_list in the callback can be recorded in callback_data (custom callback data). Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| tagging_list | Yes | Explanation: Tag list. Restrictions: None Value range: None Default value: None | |
| tag_number | unsigned int | Yes | Explanation: Number of tags. Restrictions: None Value range: [0, 10] Default value: None |
| callback_data | void * | Yes | Explanation: Pointer to the custom callback data. Restrictions: None Value range: None Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| name | char * | Yes if used as a request parameter | Explanation: Tag key. Restrictions:
Default value: None Value range: None Default value: None |
| value | char * | Yes if used as a request parameter | Explanation: Tag value. Restrictions: Tag values can be duplicated or left blank.
Default value: None Value range: None Default value: None |
Sample Code
#include "eSDKOBS.h"
#include <stdio.h>
// The response callback function. The content of properties in the callback can be recorded in callback_data (custom callback data).
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// The callback function for the completed response. The content of obs_status and obs_error_details in the callback can be recorded in callback_data (custom callback data).
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
obs_status get_object_tagging_callback(int tagging_count, obs_name_value *tagging_list, void *callback_data);
typedef struct tagkv
{
char key[250];
char value[250];
}tagkv;
typedef struct TaggingInfo
{
int tagCount;
tagkv taglist[10];
obs_status ret_status;
}TaggingInfo;
int main()
{
// The following example shows how to use get_object_tagging to obtain object tags:
// Call the obs_initialize method at the program entry to initialize global resources such as the network and memory.
obs_initialize(OBS_INIT_ALL);
obs_options options;
// Create and initialize options, including the access domain name (host_name), access keys (access_key_id and access_key_secret), bucket name (bucket_name), and bucket storage class (storage_class).
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");
// Specify the bucket name, for example, example-bucket-name.
char * bucketName = "example-bucket-name";
options.bucket_options.bucket_name = bucketName;
obs_response_handler response_handler = { &response_properties_callback, &response_complete_callback };
// Set the response callback function.
obs_get_object_tagging_handler get_object_tagging_handler =
{
response_handler,
&get_object_tagging_callback
};
// Create callback data.
TaggingInfo tagging_info;
memset(&tagging_info, 0, sizeof(TaggingInfo));
tagging_info.ret_status = OBS_STATUS_BUTT;
// Obtain object tags.
get_object_tagging(&options, "objectname", &get_object_tagging_handler, &tagging_info);
if (OBS_STATUS_OK == tagging_info.ret_status) {
printf("get object tagging successfully.\n");
}
else
{
printf("get object tagging failed(%s).\n", obs_get_status_name(tagging_info.ret_status));
}
// Release the allocated global resources.
obs_deinitialize();
}
void printTagInfo(TaggingInfo* infoToPrint)
{
int i;
printf("tag number is %d\n", infoToPrint->tagCount);
for (i = 0; i < infoToPrint->tagCount; i++)
{
printf("key:[%s], value[%s]\n", infoToPrint->taglist[i].key, infoToPrint->taglist[i].value);
}
}
obs_status get_object_tagging_callback(int tagging_count, obs_name_value *tagging_list, void *callback_data)
{
int tag_num = 0;
TaggingInfo * tag_info = (TaggingInfo *)callback_data;
tag_info->tagCount = tagging_count;
if (tagging_count > 0)
{
for (tag_num = 0; tag_num < tagging_count; tag_num++)
{
memcpy_s(tag_info->taglist[tag_num].key, sizeof(tag_info->taglist[tag_num].key), (&tagging_list[tag_num])->name, strlen((&tagging_list[tag_num])->name) + 1);
memcpy_s(tag_info->taglist[tag_num].value, sizeof(tag_info->taglist[tag_num].value), (&tagging_list[tag_num])->value, strlen((&tagging_list[tag_num])->value) + 1);
}
}
printTagInfo(tag_info);
return OBS_STATUS_OK;
}
// The response callback function. The content of properties in the callback can be recorded in callback_data (custom callback data).
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
if (properties == NULL)
{
printf("error! obs_response_properties is null!");
return OBS_STATUS_OK;
}
// Print the response.
#define print_nonnull(name, field) \
do { \
if (properties-> field) { \
printf("%s: %s\n", name, properties->field); \
} \
} while (0)
print_nonnull("request_id", request_id);
print_nonnull("request_id2", request_id2);
print_nonnull("content_type", content_type);
if (properties->content_length) {
printf("content_length: %llu\n", properties->content_length);
}
print_nonnull("server", server);
print_nonnull("ETag", etag);
print_nonnull("version_id", version_id);
return OBS_STATUS_OK;
}
// The callback function for the completed response. The content of obs_status and obs_error_details in the callback can be recorded in callback_data (custom callback data).
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
if (callback_data) {
TaggingInfo *taggingInfo = (TaggingInfo *)callback_data;
taggingInfo->ret_status = status;
}
else {
printf("Callback_data is NULL");
}
if (error && error->message) {
printf("Error Message: \n %s\n", error->message);
}
if (error && error->resource) {
printf("Error Resource: \n %s\n", error->resource);
}
if (error && error->further_details) {
printf("Error further_details: \n %s\n", error->further_details);
}
if (error && error->extra_details_count) {
int i;
for (i = 0; i < error->extra_details_count; i++) {
printf("Error Extra Detail(%d):\n %s:%s\n", i, error->extra_details[i].name,
error->extra_details[i].value);
}
}
if (error && error->error_headers_count) {
int i;
for (i = 0; i < error->error_headers_count; i++) {
const char *errorHeader = error->error_headers[i];
printf("Error Headers(%d):\n %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
}
}
} Helpful Links
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