Program.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Aliyun.OSS;
  2. using Aliyun.OSS.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace ConsoleApp5AliOSS
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. // Endpoint以杭州为例,其它Region请按实际情况填写。
  12. String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
  13. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  14. String accessKeyId = "LTAI4FsANPbYKsxD6rWB8ShJ";
  15. String accessKeySecret = "fxQ6VHiEQtwH3MjlGeCoCWYeViqzkA";
  16. String bucketName = "hystkj-oss";
  17. // 创建ClientConfiguration实例,按照您的需要修改默认参数。
  18. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  19. try
  20. {
  21. var objects = new List<string>();
  22. ObjectListing result = null;
  23. string nextMarker = string.Empty;
  24. do
  25. {
  26. var listObjectsRequest = new ListObjectsRequest(bucketName)
  27. {
  28. Marker = nextMarker,
  29. };
  30. // 列举文件。
  31. result = client.ListObjects(listObjectsRequest);
  32. foreach (var summary in result.ObjectSummaries)
  33. {
  34. Console.WriteLine(summary.Key);
  35. objects.Add(summary.Key);
  36. }
  37. nextMarker = result.NextMarker;
  38. } while (result.IsTruncated);
  39. Console.WriteLine("List objects of bucket:{0} succeeded ", bucketName);
  40. }
  41. catch (OssException ex)
  42. {
  43. Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  44. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine("Failed with error info: {0}", ex.Message);
  49. }
  50. }
  51. }
  52. }