裁剪图像后,S3 不会存储所有图像尺寸

发布于 2024-11-26 21:32:28 字数 1196 浏览 0 评论 0原文

当我创建一个带有图像的对象时,我使用回形针 + 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" 

,一切都很顺利(4 个不同尺寸的图像副本存储在我的 S3 存储桶中)

当我使用 JCrop 裁剪图像时,问题就出现了,S3 存储了 4 个副本,但是裁剪后的图像具有相同的尺寸,实际上是大尺寸。

我的控制器.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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-12-03 21:32:28

它的工作原理如下:

  • 将文件保存到亚马逊 s3
  • 通过 CDN 请求文件 如果
  • 文件可用,CDN 检查其缓存
  • 如果不可用,则从 s3 请求文件,缓存并提供服务
  • 如果可用,则从缓存提供服务。此步骤可确保您的用户获得最佳体验。如果CDN每次都从s3获取文件,那么比用户直接从s3获取文件需要更长的时间,这将违背CDN的目的。
  • 您裁剪图像并将修改后的文件保存到 s3
  • 您向 CDN 请求文件,期望它会给您裁剪后的文件
  • CDN 检查文件的缓存,找到它并从缓存中提供文件。它不会再次询问 s3 文件是否被修改

现在,我能想到的最佳方法是使用新文件名上传裁剪后的图像。这样,当 CDN 检查其缓存时,它不会在那里找到该文件,而是向 s3 请求该文件。您可以使用delayed_job或resque在后台删除旧文件。

希望它能澄清事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文