SetBucketWebsiteCommand.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Common.Communication;
  11. using Aliyun.OSS.Util;
  12. using Aliyun.OSS.Transform;
  13. namespace Aliyun.OSS.Commands
  14. {
  15. internal class SetBucketWebsiteCommand : OssCommand
  16. {
  17. private readonly string _bucketName;
  18. private readonly SetBucketWebsiteRequest _setBucketWebsiteRequest;
  19. protected override HttpMethod Method
  20. {
  21. get { return HttpMethod.Put; }
  22. }
  23. protected override string Bucket
  24. {
  25. get { return _bucketName; }
  26. }
  27. private SetBucketWebsiteCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
  28. string bucketName, SetBucketWebsiteRequest setBucketWebsiteRequest)
  29. : base(client, endpoint, context)
  30. {
  31. OssUtils.CheckBucketName(bucketName);
  32. if (string.IsNullOrEmpty(setBucketWebsiteRequest.IndexDocument))
  33. throw new ArgumentException("index document must not be empty");
  34. if (!OssUtils.IsWebpageValid(setBucketWebsiteRequest.IndexDocument))
  35. throw new ArgumentException("Invalid index document, must be end with .html");
  36. if (!string.IsNullOrEmpty(setBucketWebsiteRequest.ErrorDocument)
  37. && !OssUtils.IsWebpageValid(setBucketWebsiteRequest.ErrorDocument))
  38. throw new ArgumentException("Invalid error document, must be end with .html");
  39. _bucketName = bucketName;
  40. _setBucketWebsiteRequest = setBucketWebsiteRequest;
  41. }
  42. public static SetBucketWebsiteCommand Create(IServiceClient client, Uri endpoint,
  43. ExecutionContext context,
  44. string bucketName, SetBucketWebsiteRequest setBucketWebsiteRequest)
  45. {
  46. return new SetBucketWebsiteCommand(client, endpoint, context, bucketName, setBucketWebsiteRequest);
  47. }
  48. protected override IDictionary<string, string> Parameters
  49. {
  50. get
  51. {
  52. return new Dictionary<string, string>()
  53. {
  54. { RequestParameters.SUBRESOURCE_WEBSITE, null }
  55. };
  56. }
  57. }
  58. protected override Stream Content
  59. {
  60. get
  61. {
  62. return SerializerFactory.GetFactory().CreateSetBucketWebsiteRequestSerializer()
  63. .Serialize(_setBucketWebsiteRequest);
  64. }
  65. }
  66. }
  67. }