存储桶操作

最近更新时间:2019-02-14 11:42:09

简介

本文档提供关于存储桶的基本操作和访问控制列表(ACL)的相关 API 概览以及 SDK 示例代码。

基本操作

API 操作名 操作描述
GET Service 获取存储桶列表 获取指定账号下所有的存储桶列表
PUT Bucket 创建存储桶 在指定账号下创建一个存储桶
HEAD Bucket 检索存储桶及其权限 检索存储桶是否存在且是否有权限访问
GET Bucket location 获取存储桶地域信息 获取存储桶所在的地域信息
DELETE Bucket 删除存储桶 删除指定账号下的空存储桶

访问控制列表(ACL)

API 操作名 操作描述
PUT Bucket acl 设置存储桶 ACL 设置存储桶的 ACL 配置
GET Bucket acl 获取存储桶 ACL 获取存储桶的 ACL 配置

基本操作

获取存储桶列表

功能说明

用来获取指定账号下所有存储桶列表(Bucket list)。

方法原型

GetServiceResult GetService(GetServiceRequest request);

void GetService(GetServiceRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    GetServiceRequest request = new GetServiceRequest();
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    GetServiceResult result = cosXml.GetService(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{    
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
GetServiceRequest request = new GetServiceRequest();
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetService(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        GetServiceResult result = cosResult as GetServiceResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
});
*/

参数说明

参数名称 设置方法 描述 类型
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 GetServiceResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败
listAllMyBuckets ListAllMyBuckets 返回指定账号下的存储桶列表的信息

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。

创建存储桶

功能说明

创建一个存储桶(Put Bucket)。

方法原型

PutBucketResult PutBucket(PutBucketRequest request);

void PutBucket(PutBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    PutBucketRequest request = new PutBucketRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    PutBucketResult result = cosXml.PutBucket(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
PutBucketRequest request = new PutBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.PutBucket(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        PutBucketResult result = cosResult as PutBucketResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
});
*/

参数说明

参数名称 设置方法 描述 类型
bucket 构造方法 存储桶名称,格式:BucketName-APPID string
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 PutBucketResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。

检索存储桶

功能说明

检索指定存储桶是否存在(Head Bucket)。

方法原型

HeadBucketResult HeadBucket(HeadBucketRequest request);

void HeadBucket(HeadBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    HeadBucketRequest request = new HeadBucketRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    HeadBucketResult result = cosXml.HeadBucket(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{    
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
HeadBucketRequest request = new HeadBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.HeadBucket(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        HeadBucketResult result = cosResult as HeadBucketResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
    });
*/

获取存储桶地域

功能说明

获取指定存储桶所在的地域信息(Get Bucket Location)。

方法原型

GetBucketLocationResult GetBucketLocation(GetBucketLocationRequest request);

void GetBucketLocation(GetBucketLocationRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    GetBucketLocationRequest request = new GetBucketLocationRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    GetBucketLocationResult result = cosXml.GetBucketLocation(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{    
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
GetBucketLocationRequest request = new GetBucketLocationRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetBucketLocation(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        GetBucketLocationResult result = cosResult as GetBucketLocationResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {    
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
    });
*/

参数说明

参数名称 设置方法 描述 类型
bucket 构造方法 存储桶名称,格式:BucketName-APPID string
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 GetBucketLocationResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败
locationConstraint LocationConstraint 返回 Bucket 地域信息

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。

删除存储桶

功能说明

删除指定的存储桶(Delete Bucket)。

方法原型

DeleteBucketResult DeleteBucket(DeleteBucketRequest request);

void DeleteBucket(DeleteBucketRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    DeleteBucketRequest request = new DeleteBucketRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    DeleteBucketResult result = cosXml.DeleteBucket(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
DeleteBucketRequest request = new DeleteBucketRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.DeleteBucket(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        DeleteBucketResult result = cosResult as DeleteBucketResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
});
*/

参数说明

参数名称 设置方法 描述 类型
bucket 构造方法 存储桶名称,格式:BucketName-APPID string
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 DeleteBucketResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。

访问控制列表

设置存储桶 ACL

功能说明

设置指定存储桶访问权限控制列表(ACL)(Put Bucket ACL)。

方法原型

PutBucketACLResult PutBucketACL(PutBucketACLRequest request);

void PutBucketACL(PutBucketACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    PutBucketACLRequest request = new PutBucketACLRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //设置私有读写权限
    request.SetCosACL(CosACL.PRIVATE);
    //授予1131975903账号读权限
    COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
    readAccount.AddGrantAccount("1131975903", "1131975903");
    request.SetXCosGrantRead(readAccount);
    //执行请求
    PutBucketACLResult result = cosXml.PutBucketACL(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
PutBucketACLRequest request = new PutBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
//设置私有读写权限
request.SetCosACL(CosACL.PRIVATE);
//授予1131975903账号读权限
COSXML.Model.Tag.GrantAccount readAccount = new COSXML.Model.Tag.GrantAccount();
readAccount.AddGrantAccount("1131975903", "1131975903");
request.SetXCosGrantRead(readAccount);
//执行请求
cosXml.PutBucketACL(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        PutBucketACLResult result = cosResult as PutBucketACLResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
    });
*/

参数说明

参数名称 设置方法 描述 类型
bucket 构造方法 存储桶名称,格式:BucketName-APPID string
cosAcl SetCosAcl 设置存储桶的 ACL 权限 string
grandtAccout SetXCosGrantRead 或 SetXCosGrantWrite 或 SetXCosReadWrite 授予用户读写权限 GrantAccount
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 PutBucketACLResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。

获取存储桶 ACL

功能说明

获取指定存储桶的访问权限控制列表(ACL)(Get Bucket ACL)。

方法原型

GetBucketACLResult GetBucketACL(GetBucketACLRequest request);

void GetBucketACL(GetBucketACLRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

请求示例

try
{
    string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    GetBucketACLRequest request = new GetBucketACLRequest(bucket);
    //设置签名有效时长
    request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
    //执行请求
    GetBucketACLResult result = cosXml.GetBucketACL(request);
    //请求成功
    Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{    
    //请求失败
    Console.WriteLine("CosClientException: " + clientEx.Message);
}
catch (COSXML.CosException.CosServerException serverEx)
{
    //请求失败
    Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

/**
//异步方法
string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
GetBucketACLRequest request = new GetBucketACLRequest(bucket);
//设置签名有效时长
request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
cosXml.GetBucketACL(request, 
    delegate(COSXML.Model.CosResult cosResult)
    {
        //请求成功
        GetBucketACLResult result = cosResult as GetBucketACLResult;
        Console.WriteLine(result.GetResultInfo());

    }, 
    delegate(COSXML.CosException.CosClientException clientEx, COSXML.CosException.CosServerException serverEx)
    {    
        //请求失败
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx.Message);
        }
        else if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
    });
*/

参数说明

参数名称 设置方法 描述 类型
bucket 构造方法 存储桶名称,格式:BucketName-APPID string
signStartTimeSecond SetSign 签名有效期起始时间 long
durationSecond SetSign 签名有效期时长 long
headerKeys SetSign 签名是否校验 header List<string>
queryParameterKeys SetSign 签名是否校验请求 url 中查询参数 List<string>

返回结果说明

通过 GetBucketACLResult 返回请求结果。

成员变量 类型 描述
httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败
accessControlPolicy AccessControlPolicy 返回 Bucket 访问权限列表信息

说明:

操作失败时,系统将抛出 CosClientExceptionCosServerException 异常。