ExecutionContextBuilder.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System.Collections.Generic;
  8. using Aliyun.OSS.Common.Authentication;
  9. using Aliyun.OSS.Common.Communication;
  10. using Aliyun.OSS.Common.Handlers;
  11. namespace Aliyun.OSS.Util
  12. {
  13. internal class ExecutionContextBuilder
  14. {
  15. public ICredentials Credentials { get; set; }
  16. public IList<IResponseHandler> ResponseHandlers { get; private set; }
  17. public HttpMethod Method { get; set; }
  18. public string Bucket { get; set; }
  19. public string Key { get; set; }
  20. public ExecutionContextBuilder()
  21. {
  22. ResponseHandlers = new List<IResponseHandler>();
  23. }
  24. public ExecutionContext Build()
  25. {
  26. var context = new ExecutionContext
  27. {
  28. Signer = CreateSigner(Bucket, Key),
  29. Credentials = Credentials
  30. };
  31. foreach(var h in ResponseHandlers)
  32. {
  33. context.ResponseHandlers.Add(h);
  34. }
  35. return context;
  36. }
  37. private static IRequestSigner CreateSigner(string bucket, string key)
  38. {
  39. var resourcePath = "/" + (bucket ?? string.Empty) +
  40. ((key != null ? "/" + key : ""));
  41. // Hacked. the sign path is /bucket/key for two-level-domain mode
  42. // but /bucket/key/ for the three-level-domain mode.
  43. if (bucket != null && key == null)
  44. {
  45. resourcePath = resourcePath + "/";
  46. }
  47. return new OssRequestSigner(resourcePath);
  48. }
  49. }
  50. }