在C#中更新云前端分发的默认根对象

时间:2018-03-23 17:21:43

标签: c# amazon-web-services amazon-cloudfront

我有一个简单的C#windows窗体应用程序。我想要完成的是修改我的一个云前端发行版的默认根对象。我似乎找不到任何描述如何实现这一目标的相关文章。请帮忙

1 个答案:

答案 0 :(得分:0)

经过多次试验和错误以下是用于更新AWS云端分发的C#代码。您将需要AWS的新模块化组件。 AWSSKD.Core v3& AWSSDK.Cloudfront。

首先,您需要获得当前分布,原因有两个。主要用于验证,您需要获取Etag和来电参考。 Etag to var和Caller Reference to string。

    var client2 = new AmazonCloudFrontClient();
            var tag = client2.GetDistributionConfig(new GetDistributionConfigRequest
            {
                Id = "YOURDISTID"
            }).ETag;

            string cf = client2.GetDistributionConfig(new GetDistributionConfigRequest
            {
                Id = "YOURDISTID"
            }).DistributionConfig.CallerReference;
            client2.Dispose();

接下来,您需要对分发进行更新。我在下面提到的是更新发行版所需的最低要求(在编辑发行版时,您在AWS控制台中看到的几乎所有内容。

注意使用tag变量和cf字符串的位置。如果Etag不匹配,您将收到400个错误请求。

    var client = new AmazonCloudFrontClient();        
            client.UpdateDistribution(new UpdateDistributionRequest
            {
                Id = "YOURDISTID",
                DistributionConfig = new DistributionConfig
                {
                    WebACLId = "",
                    HttpVersion = "http2",
                    IsIPV6Enabled = true,
                    DefaultRootObject = "maintenance.html",
                    CacheBehaviors = new CacheBehaviors {
                        Quantity = 0,                            
                    },
                    Restrictions = new Restrictions {
                        GeoRestriction = new GeoRestriction
                        {
                            Quantity = 0,
                            RestrictionType = "none"
                        }
                    },
                    CustomErrorResponses = new CustomErrorResponses {
                        Quantity = 0                            
                    },                     
                    ViewerCertificate = new ViewerCertificate {
                        SSLSupportMethod = "sni-only",
                        ACMCertificateArn = "YOUR_IMPORTED_CERT_ARN",
                        MinimumProtocolVersion = "TLSv1.1_2016"                           
                    },
                    Enabled = true,
                    Comment = "Maintenance",
                    Origins = new Origins
                    {
                        Items = new List<Origin>() {
                            new Origin(){Id = "S3-example.example.com", DomainName = "example.example.com.s3.amazonaws.com", S3OriginConfig = new S3OriginConfig(){ OriginAccessIdentity = "" }, OriginPath = "", CustomHeaders = new CustomHeaders{ Quantity = 0 } }
                        },
                        Quantity = 1                           
                    },
                    Logging = new Amazon.CloudFront.Model.LoggingConfig
                    {
                        Bucket = "example.example.com.s3.amazonaws.com",
                        IncludeCookies = false,
                        Enabled = false,
                        Prefix = ""
                    },
                    PriceClass = "PriceClass_All",
                    Aliases = new Aliases
                    {                         
                        Quantity = 1,
                        Items = list
                    },
                    CallerReference = cf,
                    DefaultCacheBehavior = new DefaultCacheBehavior
                    {
                       ForwardedValues = new ForwardedValues
                       {
                           QueryString = false,
                           QueryStringCacheKeys = new QueryStringCacheKeys {
                               Quantity = 0
                           },
                           Headers = new Headers {
                               Quantity = 0                                  
                           },
                           Cookies = new CookiePreference
                           {
                               Forward = "none"                                   
                           }
                       },
                       AllowedMethods = new AllowedMethods {
                           Quantity = 2,
                           Items = httpmeth,
                           CachedMethods = new CachedMethods
                           {
                               Quantity = 2,
                               Items = httpmeth
                           }
                       },
                       DefaultTTL = 86400,
                       Compress = false,
                       MaxTTL = 31536000,
                       TargetOriginId = "S3-example.example.com",
                        LambdaFunctionAssociations = new LambdaFunctionAssociations {
                           Quantity = 0
                       },                          
                       ViewerProtocolPolicy = "allow-all", 
                       MinTTL = 0,
                       SmoothStreaming = false,
                       TrustedSigners = new TrustedSigners
                       {
                           Enabled = false,
                           Quantity = 0,                                                            
                       },               
                    }
                },
                IfMatch = tag
            });
            client.Dispose();

如果您在参考装配中找不到ACMCertificationARN的问题,您可能正在使用旧的v2 AWSSDK。删除\卸载它。获取AWSSKD.Core &amp;的最新Nuget套餐AWSSDK.CloudFront

Nuget Package Manager控制台安装:

    Install-Package AWSSDK.Core -Version 3.3.21.17
    Install-Package AWSSDK.CloudFront -Version 3.3.6.3