简介
本文介绍对象存储 COS 通过.NET(C#) SDK 实现恢复归档对象功能的示例代码和描述。
注意事项
相关示例
功能名称 | 描述 | 示例代码 |
恢复归档对象 | 一个归档存储或深度归档存储类型的对象进行恢复(解冻)以便读取该对象内容。 |
前期准备:初始化 COS 服务实例
public class DownloadObject { 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 RestoreObject() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; string key = "exampleObject"; //对象键 RestoreObjectRequest request = new RestoreObjectRequest(bucket, key); //恢复时间 request.SetExpireDays(3); request.SetTier(COSXML.Model.Tag.RestoreConfigure.Tier.Bulk); //执行请求 RestoreObjectResult result = cosXml.RestoreObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }