验证桶是否存在

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

接口定义:

void S3_test_bucket(S3Protocol protocol, S3UriStyle uriStyle,
                    const char *accessKeyId, const char *secretAccessKey,
                    const char *securityToken, const char *hostName,
                    const char *bucketName, const char *authRegion,
                    int locationConstraintReturnSize,
                    char *locationConstraintReturn,
                    S3RequestContext *requestContext,
                    int timeoutMs,
                    const S3ResponseHandler *handler, void *callbackData);

参数:

参数名 类型 说明
protocol S3Protocol 请求使用的协议
uriStyle S3UriStyle 请求使用的URI方式
accessKeyId const char * 用户的接入证书
secretAccessKey const char * 安全证书
hostName const char * 请求使用的主机名
bucketName const char * bucket名
authRegion const char * 如果非NULL,则是用于授权签名的AWS区域
locationConstraintReturnSize int 区域位置缓冲区的大小(byte)
locationConstraintReturn char * 区域位置缓冲区
requestContext S3RequestContext * 请求参数
timeoutMs int 如果非0,则是以毫秒为单位的请求超时时间
handler const S3ResponseHandler * 回调函数
callbackData void * 回调数据

代码示例:

void test_bucket()
{
    S3ResponseHandler responseHandler =
        {
            &responsePropertiesCallback, &responseCompleteCallback};

    char locationConstraint[64];
    do
    {
        S3_test_bucket(protocolG, uriStyleG, accessKeyG, secretKeyG,
                       0, hostname, test_bucket_name, 0, sizeof(locationConstraint),
                       locationConstraint, 0, 0, &responseHandler, 0);
    } while (S3_status_is_retryable(statusG) && should_retry());

    const char *result;

    switch (statusG)
    {
    case S3StatusOK:
        // bucket exists
        result = "Exists";
        break;
    case S3StatusErrorNoSuchBucket:
    case S3StatusHttpErrorNotFound:
        result = "Does Not Exist";
        break;
    case S3StatusErrorAccessDenied:
        result = "Access Denied";
        break;
    default:
        result = 0;
        break;
    }

    if (result)
    {
        printf("%-56s  %-20s\n", "                         Bucket",
               "       Status");
        printf("--------------------------------------------------------  "
               "--------------------\n");
        printf("%-56s  %-20s\n", test_bucket_name, result);
    }
    else
    {
        printError();
    }
}