开发者

after cropping image, S3 doesn't store all image sizes

开发者 https://www.devze.com 2023-03-23 13:43 出处:网络
I storing images using paperclip + S3 has_attached_file :image, :whiny => false, :styles => { :large => \"550x340>\",

I storing images using paperclip + S3

has_attached_file :image,
                     :whiny => false,
                     :styles => { :large => "550x340>",
                                  :medium  => "165x165>",
                                  :small => "100x100>",
                                  开发者_开发百科:thumbnail => "55x55>"},
                     :processors => [:cropper],
                     :storage => :s3,
                     :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                     :path => "/:id/:style",
                     :bucket => "XXX" 

when I create an object with image every thing goes fine (4 copies of the image with 4 different sizes stored in my S3 Bucket )

The problem comes when I crop the image using JCrop, S3 store 4 copies but with the same size for the cropped image actually the large size.

My controller.rb:

def update
@deal = Deal.find(params[:id])

respond_to do |format|
  if @deal.update_attributes(params[:deal])
    format.html { redirect_to(@deal, :notice => 'Deal was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @deal.errors, :status => :unprocessable_entity }
  end
end


This is how it works:

  • You save the file to amazon s3
  • You request for the file though CDN
  • CDN checks its cache if file is available
  • If not available, request the file from s3, cache it and serve
  • If available serve from cache. This step ensures that your users get a optimal experience. If CDN gets the file from s3 every time, then it will take longer than user getting file directly from s3, which will defeat the purpose of CDN.
  • You crop the image and save modified file to s3
  • You request CDN for file expecting it will give you cropped file
  • CDN checks its cache for the file, finds it and serves the file from cache. It doesn't ask s3 again if the file was modified

Now, most optimal way I can think of is to upload the cropped image with a new file name. So that when CDN checks its cache, it doesn't find it there and asks s3 for the file. You can delete the old file in background using delayed_job or resque.

Hope, it clarifies things.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号