UploadPartCommand.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using Aliyun.OSS.Transform;
  11. using Aliyun.OSS.Util;
  12. using Aliyun.OSS.Common.Communication;
  13. namespace Aliyun.OSS.Commands
  14. {
  15. internal class UploadPartCommand : OssCommand<UploadPartResult>
  16. {
  17. private readonly UploadPartRequest _uploadPartRequest;
  18. protected override HttpMethod Method
  19. {
  20. get { return HttpMethod.Put; }
  21. }
  22. protected override string Bucket
  23. {
  24. get { return _uploadPartRequest.BucketName; }
  25. }
  26. protected override string Key
  27. {
  28. get { return _uploadPartRequest.Key; }
  29. }
  30. protected override IDictionary<string, string> Parameters
  31. {
  32. get
  33. {
  34. var parameters = base.Parameters;
  35. parameters[RequestParameters.PART_NUMBER] = _uploadPartRequest.PartNumber.ToString();
  36. parameters[RequestParameters.UPLOAD_ID] = _uploadPartRequest.UploadId;
  37. return parameters;
  38. }
  39. }
  40. protected override IDictionary<string, string> Headers
  41. {
  42. get
  43. {
  44. var headers = base.Headers;
  45. headers[HttpHeaders.ContentLength] = _uploadPartRequest.PartSize.ToString();
  46. headers[HttpHeaders.ContentMd5] = _uploadPartRequest.Md5Digest;
  47. return headers;
  48. }
  49. }
  50. protected override Stream Content
  51. {
  52. get { return _uploadPartRequest.InputStream; }
  53. }
  54. protected override bool LeaveRequestOpen
  55. {
  56. get { return true; }
  57. }
  58. private UploadPartCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
  59. IDeserializer<ServiceResponse, UploadPartResult> deserializer,
  60. UploadPartRequest uploadPartRequest)
  61. : base(client, endpoint, context, deserializer)
  62. {
  63. _uploadPartRequest = uploadPartRequest;
  64. }
  65. public static UploadPartCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
  66. UploadPartRequest uploadPartRequest)
  67. {
  68. OssUtils.CheckBucketName(uploadPartRequest.BucketName);
  69. OssUtils.CheckObjectKey(uploadPartRequest.Key);
  70. if (string.IsNullOrEmpty(uploadPartRequest.UploadId))
  71. throw new ArgumentException("uploadId should be specified");
  72. if (!uploadPartRequest.PartNumber.HasValue)
  73. throw new ArgumentException("partNumber should be specified");
  74. if (!uploadPartRequest.PartSize.HasValue)
  75. throw new ArgumentException("partSize should be specified");
  76. if (uploadPartRequest.InputStream == null)
  77. throw new ArgumentException("inputStream should be specified");
  78. if (uploadPartRequest.PartSize < 0 || uploadPartRequest.PartSize > OssUtils.MaxFileSize)
  79. throw new ArgumentException("partSize not live in valid range");
  80. if (!OssUtils.IsPartNumberInRange(uploadPartRequest.PartNumber))
  81. throw new ArgumentException("partNumber not live in valid range");
  82. if (uploadPartRequest.Md5Digest == null && uploadPartRequest.PartSize != null)
  83. {
  84. uploadPartRequest.Md5Digest = OssUtils.ComputeContentMd5(uploadPartRequest.InputStream,
  85. (long)uploadPartRequest.PartSize);
  86. }
  87. return new UploadPartCommand(client, endpoint, context,
  88. DeserializerFactory.GetFactory().CreateUploadPartResultDeserializer(uploadPartRequest.PartNumber.Value),
  89. uploadPartRequest);
  90. }
  91. }
  92. }