PutObjectCommand.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 PutObjectCommand : OssCommand<PutObjectResult>
  16. {
  17. private readonly OssObject _ossObject;
  18. protected override string Bucket
  19. {
  20. get { return _ossObject.BucketName; }
  21. }
  22. protected override string Key
  23. {
  24. get { return _ossObject.Key; }
  25. }
  26. protected override bool LeaveRequestOpen
  27. {
  28. get { return true; }
  29. }
  30. private PutObjectCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
  31. IDeserializer<ServiceResponse, PutObjectResult> deserializer,
  32. OssObject ossObject)
  33. : base(client, endpoint, context, deserializer)
  34. {
  35. _ossObject = ossObject;
  36. }
  37. protected override HttpMethod Method
  38. {
  39. get { return HttpMethod.Put; }
  40. }
  41. protected override Stream Content
  42. {
  43. get { return _ossObject.Content; }
  44. }
  45. protected override IDictionary<string, string> Headers
  46. {
  47. get
  48. {
  49. var headers = new Dictionary<string, string>();
  50. _ossObject.Metadata.Populate(headers);
  51. return headers;
  52. }
  53. }
  54. public static PutObjectCommand Create(IServiceClient client, Uri endpoint, ExecutionContext context,
  55. string bucketName, string key,
  56. Stream content, ObjectMetadata metadata)
  57. {
  58. OssUtils.CheckBucketName(bucketName);
  59. OssUtils.CheckObjectKey(key);
  60. if (content == null)
  61. throw new ArgumentNullException("content");
  62. var ossObject = new OssObject(key)
  63. {
  64. BucketName = bucketName,
  65. Content = content,
  66. Metadata = metadata ?? new ObjectMetadata()
  67. };
  68. return new PutObjectCommand(client, endpoint, context,
  69. DeserializerFactory.GetFactory().CreatePutObjectReusltDeserializer(),
  70. ossObject);
  71. }
  72. }
  73. }