Invalid paths using CloudFront create invalidation in C#

时光怂恿深爱的人放手 提交于 2021-01-27 05:57:30

问题


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?


回答1:


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.




回答2:


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.




回答3:


Have you tried adding the forward slash at the beginning of the path? (/images/temp_image.jpg)



来源:https://stackoverflow.com/questions/25382449/invalid-paths-using-cloudfront-create-invalidation-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!