Search code examples
amazon-web-servicesamazon-s3cdnamazon-cloudfront

Cloudfront Error when uploading images to s3 "No Such Key"


I am developing an iOS app which will allow users to update and change their avatar where the images are being stored in S3 in subdirectories depending on their user id. Eg: "/1005/avatar_3dseuori.jpg".

I have setup cloudfront to distribute these images stored in my S3 bucket, however I am getting the following response:

<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>

even though the image exists in my bucket. If I wait ~5 minutes, I am then able to see the image using my cloudfront origin. Is this a usual waiting period?


Solution

  • CloudFront is acting as a front-end cache to your S3 origin.

    When you request the image from CloudFront, it will check it's cache, and if it does not exist in the cache (and has not expired based on the configured TTL), only then will it request the image from your S3 origin bucket.

    If you request the image from CloudFront before the image exists in S3, then CloudFront will cache the NoSuchKey result. This is because the object actually does not exist yet in S3.

    After uploading the file to S3, CloudFront will continue to respond with the cached NoSuchKey value until the configured time-to-live expires. In your case, that may be "5 minutes". After that time period, it re-requests the image from S3, this time with a positive result.

    To avoid this issue, ensure you prevent requests to CloudFront for images that do not exist.