创建桶

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

接口定义:

void S3_create_bucket(S3Protocol protocol, const char *accessKeyId,
                      const char *secretAccessKey, const char *securityToken,
                      const char *hostName, const char *bucketName,
                      const char *authRegion, S3CannedAcl cannedAcl,
                      const char *locationConstraint,
                      S3RequestContext *requestContext,
                      int timeoutMs,
                      const S3ResponseHandler *handler, void *callbackData);

参数:

参数名 类型 说明
protocol S3Protocol 请求使用的协议
accessKeyId const char * 用户的accessKey
secretAccessKey const char * 用户的secretKey
hostName const char * 请求的主机名
bucketName const char * bucket名称
authRegion const char * 如果非NULL,则是用于授权签名的AWS区域
cannedAcl S3CannedAcl 设定的权限
locationConstraint const char * 如果非NULL,则是要创建的桶的地理位置
requestContext S3RequestContext * 请求参数,如果为NULL,则立即同步执行请求
timeoutMs int 如果非0,则是以毫秒为单位的请求超时时间
handler const S3ResponseHandler * 回调函数
callbackData void * 回调数据

代码示例:

void create_bucket()
{
    if ((S3_validate_bucket_name(test_bucket_name, S3UriStyleVirtualHost) != S3StatusOK))
    {
        fprintf(stderr, "\nWARNING: Bucket name is not valid for "
                        "virtual-host style URI access.\n");
        fprintf(stderr, "Bucket not created.  Use -f option to force the "
                        "bucket to be created despite\n");
        fprintf(stderr, "this warning.\n\n");
        exit(-1);
    }

    const char *locationConstraint = 0;
    S3CannedAcl cannedAcl = S3CannedAclPrivate;

    S3ResponseHandler responseHandler =
        {
            &responsePropertiesCallback, &responseCompleteCallback};

    do
    {
        S3_create_bucket(protocolG, accessKeyG, secretKeyG, 0, hostname,
                         test_bucket_name, authRegionG, cannedAcl, locationConstraint,
                         0, 0, &responseHandler, 0);
    } while (S3_status_is_retryable(statusG));

    if (statusG == S3StatusOK)
    {
        printf("Bucket successfully created.\n");
    }
    else
    {
        printError();
    }
}