Program.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using COSXML;
  2. using COSXML.Auth;
  3. using COSXML.Common;
  4. using COSXML.Utils;
  5. using COSXML.Model;
  6. using COSXML.Model.Object;
  7. using COSXML.Model.Bucket;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using COSXML.CosException;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using COSXML.Transfer;
  16. namespace COSXMLDemo
  17. {
  18. public class Program
  19. {
  20. internal static string bucket = @"bucket-4-dotnet-demo-1253653367";
  21. public static async Task Main(string[] args)
  22. {
  23. // 腾讯云 SecretId
  24. string secretId = Environment.GetEnvironmentVariable("SECRET_ID");
  25. // 腾讯云 SecretKey
  26. string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY");
  27. // 存储桶所在地域
  28. string region = "ap-guangzhou";
  29. // 普通初始化方式
  30. CosXmlConfig config = new CosXmlConfig.Builder()
  31. .SetRegion(region)
  32. .SetDebugLog(true)
  33. .Build();
  34. // TCE 初始化方式
  35. // string domain = "your.domain"; // 替换成您的 Domain
  36. // string endpoint = String.Format("cos.{0}.{1}", region, domain);
  37. // CosXmlConfig config = new CosXmlConfig.Builder()
  38. // .setEndpointSuffix(endpoint)
  39. // .SetRegion(region)
  40. // .SetDebugLog(true)
  41. // .Build();
  42. long keyDurationSecond = 600;
  43. QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, keyDurationSecond);
  44. // service 初始化完成
  45. CosXmlServer cosXml = new CosXmlServer(config, qCloudCredentialProvider);
  46. try
  47. {
  48. // 创建存储痛
  49. Console.WriteLine(" ======= Put Bucket ======");
  50. PutBucket(cosXml);
  51. // 上传对象
  52. Console.WriteLine(" ======= Put Object ======");
  53. string cosKey = await PutObject(cosXml);
  54. // 下载对象
  55. Console.WriteLine(" ======= Get Object ======");
  56. await GetObject(cosXml, cosKey);
  57. // 删除对象
  58. Console.WriteLine(" ======= Delete Object ======");
  59. DeleteObject(cosXml, cosKey);
  60. // Console.WriteLine(" ======= Put Directory ======");
  61. // UploadDirectory(cosXml);
  62. }
  63. catch (COSXML.CosException.CosClientException clientEx)
  64. {
  65. Console.WriteLine("CosClientException: " + clientEx.Message);
  66. }
  67. catch (COSXML.CosException.CosServerException serverEx)
  68. {
  69. Console.WriteLine("CosServerException: " + serverEx.GetInfo());
  70. }
  71. finally
  72. {
  73. // 删除存储桶
  74. Console.WriteLine(" ======= Delete Bucket ======");
  75. DeleteBucket(cosXml);
  76. }
  77. Console.WriteLine(" ======= Program End. ======");
  78. }
  79. internal static void PutBucket(CosXmlServer cosXml)
  80. {
  81. try
  82. {
  83. PutBucketRequest request = new PutBucketRequest(bucket);
  84. //执行请求
  85. PutBucketResult result = cosXml.PutBucket(request);
  86. Console.WriteLine(result.GetResultInfo());
  87. }
  88. catch (COSXML.CosException.CosServerException serverEx)
  89. {
  90. if (serverEx.statusCode != 409)
  91. {
  92. throw serverEx;
  93. }
  94. else
  95. {
  96. Console.WriteLine("Bucket Already exists.");
  97. }
  98. }
  99. }
  100. internal static void DeleteBucket(CosXmlServer cosXml)
  101. {
  102. DeleteBucketRequest request = new DeleteBucketRequest(bucket);
  103. DeleteBucketResult result = cosXml.DeleteBucket(request);
  104. Console.WriteLine(result.GetResultInfo());
  105. }
  106. internal static async Task<String> PutObject(CosXmlServer cosXml)
  107. {
  108. string cosKey = "cosKey";
  109. //.cssg-snippet-body-start:[transfer-upload-file]
  110. // 初始化 TransferConfig
  111. TransferConfig transferConfig = new TransferConfig();
  112. // 初始化 TransferManager
  113. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  114. //对象在存储桶中的位置标识符,即称对象键
  115. String cosPath = cosKey;
  116. //本地文件绝对路径
  117. String srcPath = @"本地绝对路径";
  118. // 上传对象
  119. COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
  120. uploadTask.SetSrcPath(srcPath);
  121. uploadTask.progressCallback = delegate (long completed, long total)
  122. {
  123. Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  124. };
  125. try
  126. {
  127. COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
  128. transferManager.UploadAsync(uploadTask);
  129. Console.WriteLine(result.GetResultInfo());
  130. string eTag = result.eTag;
  131. }
  132. catch (Exception e)
  133. {
  134. Console.WriteLine("CosException: " + e);
  135. }
  136. return cosKey;
  137. }
  138. internal static void UploadDirectory(CosXmlServer cosXml)
  139. {
  140. //.cssg-snippet-body-start:[transfer-upload-file]
  141. // 初始化 TransferConfig
  142. TransferConfig transferConfig = new TransferConfig();
  143. // 初始化 TransferManager
  144. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  145. //本地文件夹绝对路径
  146. String dir = @"本地文件夹绝对路径";
  147. var files = System.IO.Directory.GetFiles(dir);
  148. var tasks = new List<Task>();
  149. foreach (var file in files)
  150. {
  151. Console.WriteLine("Enqueue Upload: " + file);
  152. //对象在存储桶中的位置标识符,即称对象键
  153. String cosPath = new FileInfo(file).Name;
  154. // 上传对象
  155. COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
  156. uploadTask.SetSrcPath(file);
  157. tasks.Add(transferManager.UploadAsync(uploadTask));
  158. }
  159. try
  160. {
  161. // Wait for all the tasks to finish.
  162. Task.WaitAll(tasks.ToArray());
  163. // We should never get to this point
  164. Console.WriteLine("Upload Directory Complete");
  165. }
  166. catch (AggregateException e)
  167. {
  168. Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)");
  169. for (int j = 0; j < e.InnerExceptions.Count; j++)
  170. {
  171. Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
  172. }
  173. }
  174. }
  175. internal static async Task GetObject(CosXmlServer cosXml, string cosKey)
  176. {
  177. TransferConfig transferConfig = new TransferConfig();
  178. // 初始化 TransferManager
  179. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  180. //对象在存储桶中的位置标识符,即称对象键
  181. String cosPath = cosKey;
  182. //本地文件夹
  183. string localDir = System.IO.Path.GetTempPath();
  184. //指定本地保存的文件名
  185. string localFileName = "my-local-temp-file";
  186. // 下载对象
  187. COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
  188. localDir, localFileName);
  189. downloadTask.progressCallback = delegate (long completed, long total)
  190. {
  191. Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  192. };
  193. try
  194. {
  195. COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await
  196. transferManager.DownloadAsync(downloadTask);
  197. Console.WriteLine(result.GetResultInfo());
  198. string eTag = result.eTag;
  199. }
  200. catch (Exception e)
  201. {
  202. Console.WriteLine("CosException: " + e);
  203. }
  204. }
  205. internal static void DeleteObject(CosXmlServer cosXml, string cosKey)
  206. {
  207. DeleteObjectRequest request = new DeleteObjectRequest(bucket, cosKey);
  208. DeleteObjectResult result = cosXml.DeleteObject(request);
  209. Console.WriteLine(result.GetResultInfo());
  210. }
  211. }
  212. }