获取桶列表

Bucket是用于存储对象(Object)的容器,所有的对象都必须隶属于某个Bucket。本文介绍如何获取桶(Bucket)列表。

接口定义:

void S3_list_service(S3Protocol protocol, const char *accessKeyId,
                     const char *secretAccessKey, const char *securityToken,
                     const char *hostName, const char *authRegion,
                     S3RequestContext *requestContext,
                     int timeoutMs,
                     const S3ListServiceHandler *handler, void *callbackData);

参数:

参数名 类型 说明
protocol S3Protocol 请求使用的协议
accessKeyId const char * 用户的accessKey
secretAccessKey const char * 用户的secretKey
securityToken const char * 如果非NULL,则该安全令牌用于生成临时的凭证
hostName const char * 请求的主机名
authRegion const char * 如果非NULL,则是用于授权签名的AWS区域
requestContext S3RequestContext * 请求参数,如果为NULL,则立即同步执行请求
timeoutMs int 如果非0,则是以毫秒为单位的请求超时时间
handler const S3ResponseHandler * 回调函数
callbackData void * 回调数据

代码示例:

#include <unistd.h>

// 打印标识各项的头部
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");
}

void list_service(int allDetails)
{
    list_service_data data;

    data.headerPrinted = 0;
    data.allDetails = allDetails;

    S3ListServiceHandler listServiceHandler =
        {
            {&responsePropertiesCallback, &responseCompleteCallback},
            &listServiceCallback};

    do
    {
        S3_list_service(protocolG, accessKeyG, secretKeyG, 0, hostname, authRegionG,
                        NULL, 0, &listServiceHandler, &data);
        printf("%d\n", statusG);
    } while (S3_status_is_retryable(statusG) && should_retry());

    if (statusG == S3StatusOK)
    {
        if (!data.headerPrinted)
        {
            printListServiceHeader(allDetails);
        }
    }
    else
    {
        printError();
    }
}