Hilmi Yamcı
Hilmi Yamcı

Reputation: 473

Amazon CloudFront doesn't require me to invalidate objects

I have a ruby on rails application where users can upload their avatar or change it. First I stored the images in Amazon s3 but then I realized that content contents were being served slowly and decided to use Amazon cloudfront.

There is no problem for uploading and getting avatar. However, I can see that an updated photo changes immediately but I expect to invalidate it through cloudfront api. And uploading an image takes a lot of time.

At this point I can't decide whether I use cloudfront correctly or not.

This my carrierwave.rb file inside config/initializer:

 CarrierWave.configure do |config|

  config.fog_provider = 'fog/aws'                        
  config.fog_credentials = {
    provider:              'AWS',                      
    aws_access_key_id:     'key',      
    aws_secret_access_key: 'value',    
    region:                'us-east-1'
  }

  config.storage :fog
  config.asset_host     = 'http://images.my-domain.com'
  config.fog_directory  = 'bucket_name'                          
  config.fog_public     = true
  config.fog_attributes = { cache_control: "public, max-age=315576000" }
end

I can't see what I'm missing ? How can I be assure that I'm using cloudfront properly ?

Thanks.

Upvotes: 0

Views: 142

Answers (1)

Daniel Westendorf
Daniel Westendorf

Reputation: 3465

Your images aren't being stored in CloudFront, they're being served through CloudFront's CDN.

First request for an image served through CF looks like this:

Browser -> CloudFront -> S3
                          |
Browser <-  CloudFront  <-

The second request for an image just looks like this:

Browser -> CloudFront
                    |
Browser           <-  

The second request never hit's CF because CF has cached the result for that URL.

NOW, your avatar's updating immediately is simply probably because it's being uploaded to S3 and resulting in a new URL, and thusly, an immediate update. This is how you want it to work.

Upvotes: 1

Related Questions