生命周期

最后更新时间:2020-03-25 14:18:14

    简介

    本文档提供关于生命周期的 API 概览以及 SDK 示例代码。

    API 操作名 操作描述
    PUT Bucket lifecycle 设置生命周期 设置存储桶的生命周期管理的配置
    GET Bucket lifecycle 查询生命周期 查询存储桶生命周期管理的配置
    DELETE Bucket lifecycle 删除生命周期 删除存储桶生命周期管理的配置

    设置生命周期

    功能说明

    设置指定存储桶的生命周期配置信息。

    方法原型

    PutBucketLifecycleResult PutBucketLifecycle(PutBucketLifecycleRequest request);
    
    void PutBucketLifecycle(PutBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

    请求示例

    CosXmlConfig config = new CosXmlConfig.Builder()
      .SetConnectionTimeoutMs(60000)  //设置连接超时时间,单位毫秒,默认45000ms
      .SetReadWriteTimeoutMs(40000)  //设置读写超时时间,单位毫秒,默认45000ms
      .IsHttps(true)  //设置默认 HTTPS 请求
      .SetAppid("1250000000") //设置腾讯云账户的账户标识 APPID
      .SetRegion("COS_REGION") //设置一个默认的存储桶地域
      .Build();
    
    string secretId = "COS_SECRETID";   //云 API 密钥 SecretId
    string secretKey = "COS_SECRETKEY"; //云 API 密钥 SecretKey
    long durationSecond = 600;          //每次请求签名有效时长,单位为秒
    QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
      secretKey, durationSecond);
    
    CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);
    
    try
    {
      string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
      PutBucketLifecycleRequest request = new PutBucketLifecycleRequest(bucket);
      //设置签名有效时长
      request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
      //设置 lifecycle
      LifecycleConfiguration.Rule rule = new LifecycleConfiguration.Rule();
      rule.id = "lfiecycleConfigureId";
      rule.status = "Enabled"; //Enabled,Disabled
    
      rule.filter = new COSXML.Model.Tag.LifecycleConfiguration.Filter();
      rule.filter.prefix = "2/";
    
      //指定分片过期删除操作
      rule.abortIncompleteMultiUpload = new LifecycleConfiguration.AbortIncompleteMultiUpload();
      rule.abortIncompleteMultiUpload.daysAfterInitiation = 2;
    
      request.SetRule(rule);
    
      //执行请求
      PutBucketLifecycleResult result = cosXml.PutBucketLifecycle(request);
      //请求成功
      Console.WriteLine(result.GetResultInfo());
    }
    catch (COSXML.CosException.CosClientException clientEx)
    {
      //请求失败
      Console.WriteLine("CosClientException: " + clientEx);
    }
    catch (COSXML.CosException.CosServerException serverEx)
    {
      //请求失败
      Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }

    参数说明

    参数名称 设置方法 描述 类型
    bucket 构造方法 设置生命周期的存储桶,格式:BucketName-APPID string
    rule SetRule 设置存储桶的生命周期配置 LifecycleConfiguration.Rule
    signStartTimeSecond SetSign 签名有效期起始时间(Unix 时间戳),例如1557902800 long
    durationSecond SetSign 签名有效期时长(单位为秒),例如签名有效时期为1分钟:60 long
    headerKeys SetSign 签名的请求头 List<string>
    queryParameterKeys SetSign 签名的请求参数 List<string>

    返回结果说明

    通过 PutBucketLifecycleResult 返回请求结果。

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

    查询生命周期

    功能说明

    查询存储桶的生命周期管理配置。

    方法原型

    GetBucketLifecycleResult GetBucketLifecycle(GetBucketLifecycleRequest request);
    
    void GetBucketLifecycle(GetBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

    请求示例

    CosXmlConfig config = new CosXmlConfig.Builder()
      .SetConnectionTimeoutMs(60000)  //设置连接超时时间,单位毫秒,默认45000ms
      .SetReadWriteTimeoutMs(40000)  //设置读写超时时间,单位毫秒,默认45000ms
      .IsHttps(true)  //设置默认 HTTPS 请求
      .SetAppid("1250000000") //设置腾讯云账户的账户标识 APPID
      .SetRegion("COS_REGION") //设置一个默认的存储桶地域
      .Build();
    
    string secretId = "COS_SECRETID";   //云 API 密钥 SecretId
    string secretKey = "COS_SECRETKEY"; //云 API 密钥 SecretKey
    long durationSecond = 600;          //每次请求签名有效时长,单位为秒
    QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
      secretKey, durationSecond);
    
    CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);
    
    try
    {
      string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
      GetBucketLifecycleRequest request = new GetBucketLifecycleRequest(bucket);
      //设置签名有效时长
      request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
      //执行请求
      GetBucketLifecycleResult result = cosXml.GetBucketLifecycle(request);
      //存储桶的生命周期配置
      LifecycleConfiguration conf = result.lifecycleConfiguration;
    }
    catch (COSXML.CosException.CosClientException clientEx)
    {
      //请求失败
      Console.WriteLine("CosClientException: " + clientEx);
    }
    catch (COSXML.CosException.CosServerException serverEx)
    {
      //请求失败
      Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }

    参数说明

    参数名称 设置方法 描述 类型
    bucket 构造方法 查询生命周期的存储桶,格式:BucketName-APPID string
    signStartTimeSecond SetSign 签名有效期起始时间(Unix 时间戳),例如1557902800 long
    durationSecond SetSign 签名有效期时长(单位为秒),例如签名有效时期为1分钟:60 long
    headerKeys SetSign 签名的请求头 List<string>
    queryParameterKeys SetSign 签名的请求参数 List<string>

    返回结果说明

    通过 GetBucketLifecycleResult 返回请求结果。

    成员变量 类型 描述
    httpCode int HTTP Code, [200,300)之间表示操作成功,否则表示操作失败
    lifecycleConfiguration LifecycleConfiguration 返回 Bucket 的生命周期配置信息

    删除生命周期

    功能说明

    删除存储桶生命周期管理的配置。

    方法原型

    DeleteBucketLifecycleResult DeleteBucketLifecycle(DeleteBucketLifecycleRequest request);
    
    void DeleteBucketLifecycle(DeleteBucketLifecycleRequest request, COSXML.Callback.OnSuccessCallback<CosResult> successCallback, COSXML.Callback.OnFailedCallback failCallback);

    请求示例

    CosXmlConfig config = new CosXmlConfig.Builder()
      .SetConnectionTimeoutMs(60000)  //设置连接超时时间,单位毫秒,默认45000ms
      .SetReadWriteTimeoutMs(40000)  //设置读写超时时间,单位毫秒,默认45000ms
      .IsHttps(true)  //设置默认 HTTPS 请求
      .SetAppid("1250000000") //设置腾讯云账户的账户标识 APPID
      .SetRegion("COS_REGION") //设置一个默认的存储桶地域
      .Build();
    
    string secretId = "COS_SECRETID";   //云 API 密钥 SecretId
    string secretKey = "COS_SECRETKEY"; //云 API 密钥 SecretKey
    long durationSecond = 600;          //每次请求签名有效时长,单位为秒
    QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, 
      secretKey, durationSecond);
    
    CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider);
    
    try
    {
      string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
      DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest(bucket);
      //设置签名有效时长
      request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600);
      //执行请求
      DeleteBucketLifecycleResult result = cosXml.DeleteBucketLifecycle(request);
      //请求成功
      Console.WriteLine(result.GetResultInfo());
    }
    catch (COSXML.CosException.CosClientException clientEx)
    {
      //请求失败
      Console.WriteLine("CosClientException: " + clientEx);
    }
    catch (COSXML.CosException.CosServerException serverEx)
    {
      //请求失败
      Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }

    参数说明

    参数名称 设置方法 描述 类型
    bucket 构造方法 被删除生命周期配置的存储桶,格式:BucketName-APPID string
    signStartTimeSecond SetSign 签名有效期起始时间(Unix 时间戳),例如1557902800 long
    durationSecond SetSign 签名有效期时长(单位为秒),例如签名有效时期为1分钟:60 long
    headerKeys SetSign 签名的请求头 List<string>
    queryParameterKeys SetSign 签名的请求参数 List<string>

    返回结果说明

    通过 DeleteBucketLifecycleResult 返回请求结果。

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

    Was this page helpful?

    本页内容是否解决了您的问题?

    • 完全没帮助
    • 文档较差
    • 文档一般
    • 文档不错
    • 文档很好
    反馈
    帮助