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

Ida*_*ter 5 .net c# amazon-web-services cache-invalidation amazon-cloudfront

我试图使 C#/.NET 中的 CloudFront 对象无效并得到以下异常:

您的请求包含一个或多个无效失效路径。

我的功能:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

images/temp_image.jpg
Run Code Online (Sandbox Code Playgroud)

该图像存在于 S3 存储桶中并加载到浏览器中 CloudFront URL 中。

我究竟做错了什么?

小智 2

当您向 CloudFront 中的某些对象发送失效请求时,即使失效完成,您仍然可以在浏览器中的 CloudFront URL 中看到您的图片,因为失效不会从 S3 存储桶中删除对象,并且会再次从浏览器 CloudFront 中删除对该图像的新请求将这些 URl 缓存到边缘位置的 images/temp_image.jpg 中。

当您更新具有相同名称的图像时,将会看到对象失效。

您的失效函数是正确的。