AppendObjectCommand.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using Aliyun.OSS.Common.Communication;
  11. using Aliyun.OSS.Util;
  12. using Aliyun.OSS.Transform;
  13. namespace Aliyun.OSS.Commands
  14. {
  15. internal class AppendObjectCommand : OssCommand<AppendObjectResult>
  16. {
  17. private const string AppendName = "append";
  18. private const string AppendPosition = "position";
  19. private readonly AppendObjectRequest _request;
  20. protected override string Bucket
  21. {
  22. get { return _request.BucketName; }
  23. }
  24. protected override string Key
  25. {
  26. get { return _request.Key; }
  27. }
  28. protected override IDictionary<string, string> Parameters
  29. {
  30. get
  31. {
  32. var parameters = base.Parameters;
  33. parameters[AppendName] = string.Empty;
  34. parameters[AppendPosition] = _request.Position.ToString();
  35. return parameters;
  36. }
  37. }
  38. private AppendObjectCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
  39. IDeserializer<ServiceResponse, AppendObjectResult> deserializer,
  40. AppendObjectRequest request)
  41. : base(client, endpoint, context, deserializer)
  42. {
  43. _request = request;
  44. }
  45. protected override HttpMethod Method
  46. {
  47. get { return HttpMethod.Post; }
  48. }
  49. protected override Stream Content
  50. {
  51. get { return _request.Content; }
  52. }
  53. protected override bool LeaveRequestOpen
  54. {
  55. get { return true; }
  56. }
  57. protected override IDictionary<string, string> Headers
  58. {
  59. get
  60. {
  61. var headers = new Dictionary<string, string>();
  62. _request.ObjectMetadata.Populate(headers);
  63. return headers;
  64. }
  65. }
  66. public static AppendObjectCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
  67. AppendObjectRequest request)
  68. {
  69. OssUtils.CheckBucketName(request.BucketName);
  70. OssUtils.CheckObjectKey(request.Key);
  71. if (request.Content == null)
  72. throw new ArgumentNullException("request.Content");
  73. request.ObjectMetadata = request.ObjectMetadata ?? new ObjectMetadata();
  74. if (request.ObjectMetadata.ContentType == null)
  75. request.ObjectMetadata.ContentType = HttpUtils.GetContentType(request.Key, null);
  76. return new AppendObjectCommand(client, endpoint, context,
  77. DeserializerFactory.GetFactory().CreateAppendObjectReusltDeserializer(),
  78. request);
  79. }
  80. }
  81. }