获取桶列表回调

使用SDK时可能需要传入各类回调函数以完成您想要实现的相关操作,本文介绍获取桶列表回调的编写示例。

功能:

  • 用于在ListService中获取bucket列表的回调函数。

接口定义:

S3Status (S3ListServiceCallback)(const char *ownerId, 
                                 const char *ownerDisplayName,
                                 const char *bucketName,
                                 int64_t creationDateSeconds,
                                 void *callbackData);

参数:

参数名 类型 说明
ownerId const char * bucket拥有者的id
ownerDisplayName const char * bucket拥有者的显示名称
bucketName const char * bucket名字
creationDateSeconds int64_t 如果小于0,表明bucket没有提供创建时间,
反之表明bucket创建时间距Unix时间戳的秒数
callbackData void * 发出请求时指定的回调数据

代码示例:

#include <time.h>

typedef struct list_service_data
{
    int headerPrinted;
    int allDetails;
} list_service_data;

// 打印标识各项的头部
void printListServiceHeader(int allDetails)
{
    printf("%-56s  %-20s", "                         Bucket",
           "      Created");
    if (allDetails) {
        printf("  %-64s  %-12s", 
               "                            Owner ID",
               "Display Name");
    }
    printf("\n");
    printf("--------------------------------------------------------  "
           "--------------------");
    if (allDetails) {
        printf("  -------------------------------------------------"
               "---------------  ------------");
    }
    printf("\n");
}

S3Status listServiceCallback(const char *ownerId, 
                                    const char *ownerDisplayName,
                                    const char *bucketName,
                                    int64_t creationDate, void *callbackData)
{
    list_service_data *data = (list_service_data *) callbackData;

    if (!data->headerPrinted) {
        data->headerPrinted = 1;
        printListServiceHeader(data->allDetails);
    }

    char timebuf[256];
    if (creationDate >= 0) {
        time_t t = (time_t) creationDate;
        strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
    }
    else {
        timebuf[0] = 0;
    }

    printf("%-56s  %-20s", bucketName, timebuf);
    if (data->allDetails) {
        printf("  %-64s  %-12s", ownerId ? ownerId : "", 
               ownerDisplayName ? ownerDisplayName : "");
    }
    printf("\n");

    return S3StatusOK;
}