c# - 使用 CloudFront 的无效路径在 C# 中创建失效

标签 c# .net amazon-web-services amazon-cloudfront cache-invalidation

我正在尝试使 C#/.NET 中的 CloudFront 对象无效并引发以下异常:

Your request contains one or more invalid invalidation paths.

我的函数:

public bool InvalidateFiles(string[] arrayofpaths)
{
    for (int i = 0; i < arrayofpaths.Length; i++)
    {
        arrayofpaths[i] = Uri.EscapeUriString(arrayofpaths[i]);
    }

    try
    {
        Amazon.CloudFront.AmazonCloudFrontClient oClient = new Amazon.CloudFront.AmazonCloudFrontClient(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, Amazon.RegionEndpoint.USEast1);
        CreateInvalidationRequest oRequest = new CreateInvalidationRequest();
        oRequest.DistributionId = ConfigurationManager.AppSettings["CloudFrontDistributionId"];
        oRequest.InvalidationBatch = new InvalidationBatch
        {
            CallerReference = DateTime.Now.Ticks.ToString(),
            Paths = new Paths
            {
                Items = arrayofpaths.ToList<string>(),
                Quantity = arrayofpaths.Length
            }
        };

        CreateInvalidationResponse oResponse = oClient.CreateInvalidation(oRequest);
        oClient.Dispose();
    }
    catch
    {
        return false;
    }
    return true;
}

传递给函数的数组包含单个 Url,如下所示:

images/temp_image.jpg

图像存在于 S3 存储桶中,并通过 CloudFront URL 在浏览器中加载。

我做错了什么?

最佳答案

您的无效文件路径需要在字符串的前面有一个/。

如果您有疑问,可以登录 AWS Management,转到 Cloudfront,选择您尝试使文件失效的分配,选择分配设置并转到无效选项卡。

然后您可以手动创建验证,这允许您检查您的路径是否正确。

关于c# - 使用 CloudFront 的无效路径在 C# 中创建失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25382449/

相关文章:

C# 帮助 : Sorting a List of Objects in C#

c# - 为什么无法通过 PrincipalContext 联系 Active Directory 服务器?

c# - 使用 Json.NET 序列化对象列表

.net - 实现扩展方法 WebRequest.GetResponseAsync 并支持 CancellationToken

amazon-web-services - 如何从S3中的zip存档中提取文件

amazon-web-services - docker构建在aws ecs中花费了太多时间

c# - 嵌套Task.WhenAll有什么限制吗?

c#foreach循环访问变量

c# - 处理 DataContext 和代码分析消息

amazon-web-services - 使用 CloudFormation 模板基于 IAM 的 ssh 到 EC2 实例