版本控制

最近更新时间:2024-09-14 14:35:31

我的收藏

简介

本文档提供关于版本控制的 API 概览以及 SDK 示例代码。

相关示例

功能名称
描述
示例代码
设置版本控制
设置存储桶的版本控制功能
查询版本控制
查询存储桶的版本控制信息

前期准备:初始化 COS 服务实例

public class ObjectModel { private CosXml cosXml; //将服务用户设置成数据成员 // 初始化COS服务实例 private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }

使用案例

设置版本控制

设置指定存储桶的版本控制功能。开启版本控制功能后,只能暂停,不能关闭。
// 设置存储桶多版本 public void PutBucketVersioning() { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; PutBucketVersioningRequest request = new PutBucketVersioningRequest(bucket); request.IsEnableVersionConfig(true); //true: 开启版本控制; false:暂停版本控制 try { PutBucketVersioningResult result = cosXml.PutBucketVersioning(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

查询版本控制

查询指定存储桶的版本控制信息。
获取存储桶版本控制的状态,需要有该存储桶的读权限。
有三种版本控制状态:未启用版本控制、启用版本控制和暂停版本控制。
// 获取存储桶多版本状态 public void GetBucketVersioning() { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketVersioningRequest request = new GetBucketVersioningRequest(bucket); try { GetBucketVersioningResult result = cosXml.GetBucketVersioning(request); // 存储桶的生命周期配置 VersioningConfiguration conf = result.versioningConfiguration; Console.WriteLine(conf.GetInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

API 操作

关于设置版本控制的 API 接口说明,请参见 PUT Bucket versioning 文档。
关于查询版本控制的 API 接口说明,请参见 GET Bucket versioning 文档。