Search code examples
c#.netamazon-web-servicesamazon-cloudfrontcache-invalidation

Invalid paths using CloudFront create invalidation in C#


I am trying to invalidate CloudFront objects in C#/.NET and gettign the following exception:

Your request contains one or more invalid invalidation paths.

My Function:

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;
}

The array passed to the function contains a single Url like so:

images/temp_image.jpg

The image exists in the S3 bucket and loaded in the browser in the CloudFront URL.

What am I doing wrong?


Solution

  • When you send invalidation request to some object in CloudFront, you still can see your picture in the browser in the CloudFront URL even when invalidation completed, because invalidation does not delete object from S3 bucket and with new request to this image from you browser CloudFront again cached these URl to images/temp_image.jpg in edge locations.

    Invalidation of object will be seen, when you update image with the same name.

    Your Invalidation function is correct.