2017-01-22 4 views
0

최근에 내 응용 프로그램에 대한 CDN을 설정했습니다. 정적 자산, JS 및 CSS와 같은 거의 모든 것이 잘 작동합니다.Rails image_tag에서 CDN 자산을 사용하지 않습니다.

그러나 일부 개체 자산 (이미지) 예를 들어,에서 데이터를 가져 오기 위해 내 S3 URL을 계속 사용 :

<%= image_tag(@listing.cover.url(:large))%> 

가되고, 생산 :

<img src="https://musicjungle.s3.amazonaws.com/listing_covers/5045/large.jpg?1484594254" class="fotorama__img" style="width: 548px; height: 548px; left: 91px; top: 0px;"> 

대신 내 CDN을 사용. 기록을 위해, 여기에 내가 설정 CDN에 나의 production.rb의 조각이다 : 나는 바와 같이

#CDN settings 
config.action_controller.asset_host = "d1bfllp5zjnl7u.cloudfront.net" 

, 내 모든 다른 자산은 문제없이 렌더링되어 있지만, 사람들은 여전히 ​​S3를 사용합니다. 어쩌면 이건 내가 가진 첨부 도우미와 관련이 있을까요?

module Shared 
    module AttachmentHelper 

    def self.included(base) 
     base.extend ClassMethods 
    end 

    module ClassMethods 
     def has_attachment(name, options = {}) 

     # generates a string containing the singular model name and the pluralized attachment name. 
     # Examples: "user_avatars" or "asset_uploads" or "message_previews" 
     attachment_owner = self.table_name.singularize 
     attachment_folder = "#{attachment_owner}_#{name.to_s.pluralize}" 

     # we want to create a path for the upload that looks like: 
     # message_previews/00/11/22/001122deadbeef/thumbnail.png 
     attachment_path  = "#{attachment_folder}/:id/:style.:extension" 

     if Rails.env.production? 
      options[:path]   ||= attachment_path 
      options[:storage]   ||= :s3 
      options[:s3_credentials] ||= { 
      :bucket => 'bucket-name', 
      :access_key_id => 'KEY_ID', 
      :secret_access_key => 'ACCESS_KEY' 
      } 
      options[:s3_permissions] ||= 'private' 
      options[:s3_protocol]  ||= 'https' 
      options[:s3_headers]  ||= { 
      'Cache-Control' => 'max-age=315576000', 
      'Expires' => 10.years.from_now.httpdate 
      } 
     else 
      # For local Dev/Test envs, use the default filesystem, but separate the environments 
      # into different folders, so you can delete test files without breaking dev files. 
      options[:path] ||= ":rails_root/public/system/attachments/#{Rails.env}/#{attachment_path}" 
      options[:url] ||= "/system/attachments/#{Rails.env}/#{attachment_path}" 
     end 

     # pass things off to paperclip. 
     has_attached_file name, options 
     end 
    end 
    end 
end 

답변

0

당신의 종이 클립 옵션에서 s3_host_alias 옵션을 설정해보십시오 :

options[:s3_host_alias] = "d1bfllp5zjnl7u.cloudfront.net" 
나는 S3에 응용 프로그램의 이미지를 업로드하기 위해 하나를 만들어, 여기에 코드입니다