Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Idan Shechter Avatar asked Aug 19 '14 11:08

Idan Shechter


People also ask

What does CloudFront invalidation do?

CloudFront Invalidations are used to clear cached files at CloudFront Edge locations.

What is path pattern in CloudFront?

Path pattern. A path pattern (for example, images/*. jpg ) specifies which requests you want this cache behavior to apply to. When CloudFront receives an end-user request, the requested path is compared with path patterns in the order in which cache behaviors are listed in the distribution.


1 Answers

You invalidation file paths need a / at the front of the string.

If you are in doubt, you can log onto AWS Management, go to Cloudfront, select the distribution you are trying to invalidate files from, select Distribution setting and go to the Invalidations tab.

You can then create validations manually, which allows you to check that your paths are correct.

like image 141
Liam Avatar answered Sep 26 '22 17:09

Liam