Rails 6.1 ã® rails_storage_proxy_url ã§ActiveStorage ã®ãªãã¤ã¬ã¯ãURLåé¡ã解決ãã
Rails 6.1 ã®æ°æ©è½ rails_storage_proxy_url
ã使ãã¨ãActiveStorage ã§æ·»ä»ãããã¡ã¤ã«ã¸ã®ãªã³ã¯ãç½²åä»ãURLã¸ã®ãªãã¤ã¬ã¯ãã«ãªãããRailsã¢ããªã®URLã®ã¾ã¾ãã¡ã¤ã«ããã¦ã³ãã¼ãã§ããããã«ãªãã¾ãã
ã©ããããã¨ï¼
ActiveStorageã¯ããã¾ã§ãS3ãããã¯ã¨ã³ãã¨ãã¦ä½¿ã£ãå ´åãS3ã¸ã®ç½²åä»ãURL=ã¿ã¤ã ã¹ã¿ã³ããªã©ãä»ä¸ãããURLã¸ã®ãªãã¤ã¬ã¯ããè¡ã£ã¦ãã¾ããã ãããããã¯æ±ãã¥ãããã¨ãå°ãªããªããæ©ã¿ã®ç¨®ã®1ã¤ã§ããã
Rails 6.1 ã§ãã®åé¡ã«å¯¾ããåçãï¼ããããï¼å ¬å¼ã«ç¨æããããã¨ã«ãªãã¾ãã
ä¾
url_for(user.photo)
ã§ActiveStorageã¸ã®URLãçæ- ãã¨ãã°
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZDg9IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d248c202e2e205d7cff8385cceaa598ea0fe244f/photo.png
ã®ãããªã® - 1ã§çæãããURLã«ã¢ã¯ã»ã¹ããã¨ãS3ã®ç½²åä»ãURLï¼æéä»ãï¼ã«ãªãã¤ã¬ã¯ã
- ãã¨ãã°
https://myapp-development-uploads.s3.ap-northeast-1.amazonaws.com/aots0fza2jg5yzznixa4a2eb5nwg?response-content-disposition=inline%3B%20filename%3D%22photo.png%22%3B%20filename%2A%3DUTF-8%27%27photo.png&response-content-type=image%2Fpng&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA2JJO3DN3RFKWDW7Q%2F20210120%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Date=20210120T162827Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=645c971759d68c0c53dc3bdd2547838d9c03cdf6ade730777042f1cd69220d33
- S3ã®ç½²åä»ãURLãããã¡ã¤ã«ãGET
ä½ã«å°ãã
- ããã§ã¯å段ã«ãããã·ãªã©ãç½®ãã¦ãã£ãã·ã¥ãããã¨ãã§ãã¾ããã
- S3ã®ç½²åä»ãURLããã®å¿çã¯HTTPãã£ãã·ã¥ãå¹ãã¾ãããæéä»ãã§ããããæ¬æ¥ãã£ãã·ã¥ãããã¨å°ãããã§ãã®ã§ãå½ç¶ã¨è¨ãã°å½ç¶ã§ãã
- S3ã®ç½²åä»ãURLã®æéãåããã¨ã¢ã¯ã»ã¹ã§ãã¾ããã
- ãã¨ãã°ã¯ã©ã¤ã¢ã³ãã¢ããªãªã©ã§ç½²åä»ãURLããã£ãã·ã¥ãããã¨ãæéãåããã¨åèªè¾¼ãããã¨ãã¦ã¨ã©ã¼ï½¥ï½¥ï½¥ã®ãããªãã¨ããã¯ã©ã¤ã¢ã³ãã®ãã£ãã·ã¥å®è£ 次第ã§çºçãã¾ãããã¾ããã
å¤ä»£äººã®å¯¾å¿
å¤ä»£ã®äººã¯ãã®åé¡ã®è§£æ¶ã®ããããããããªå·¥å¤«ããã¾ããã
ãã¨ãã°S3ã®ãã±ããããªã·ã¼ã§ public read å¯è½ã«ãã¦ç½²åãä¸è¦ã«ããä¸ã§ user.photo.service.send(:public_url, user.photo.key)
ã¿ããã«ãã¦ã https://myapp-development-uploads.s3.ap-northeast-1.amazonaws.com/aots0fza2jg5yzznixa4a2eb5nwg
ã®ãããªURLãå¾ããã§ãã
ãã¡ããS3ã®ãããªãã¯ã¢ã¯ã»ã¹ã¯æå¹ã«ãã¹ãã§ã¯ããã¾ãã・・・
Rails 6.1 ã® rails_storage_proxy_url ã§ãã®åé¡ã¸ã®çããåºã
Rails 6.1 ã® Active Storage ã§ã¯ãæ°ãã« rails_storage_proxy_url
ãå®è£
ããã¾ããã
ããã使ãã¨ãããã¯ã¨ã³ãã¸ã®ãªãã¤ã¬ã¯ãã§ã¯ãªããRailsã¢ããªå ã§ããã¯ã¨ã³ãããã®ãã¦ã³ãã¼ããä¸ç¶ããå ãã¦HTTPãã£ãã·ã¥ãæå¹ã«ãã¦ããã¾ãã
<%= image_tag rails_storage_proxy_url(user.photo) %>
ã¨ããã¨
<img src="http://localhost:3000/rails/active_storage/blobs/proxy/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZDg9IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d248c202e2e205d7cff8385cceaa598ea0fe244f/photo.png" />
ã¨ãªãããã®URL㯠ActiveStorage ã® Controller ã«ã¤ãªãã£ã¦
def show http_cache_forever public: true do set_content_headers_from representation.image stream representation end end
rails/proxy_controller.rb at v6.1.0 · rails/rails · GitHub
ãããªãããã§ãã
ä½ãå¬ããã
ç½²åä»ãURLã§ã®ãªãã¤ã¬ã¯ããçºçããªããªã£ããã¨ã§ã主ã«ãã£ãã·ã¥ãæ±ãããããªãã¾ãã
- å段ã«ãããã·ãªã©ãç½®ãã¦ãã£ãã·ã¥ãããã¨ãã§ããããã«ãªãã¾ããã
- HTTPãã£ãã·ã¥ã使ããããã«ãªãã¾ããã
ãã¡ããå人å®ã¡ãã»ã¼ã¸ã®æ·»ä»ãã¡ã¤ã«ã®ããã«ããã¡ã¤ã«ã®æ§è³ªã«ãã£ã¦ã¯ãã£ãã·ã¥ã§ããã¨ã¾ããå ´åãããããããã£ããã®ã«ã¤ãã¦ã¯
rails_blob_url(message.attachment)
ããã㯠rails_representation_url(message.attachment.variant(strip: true))
ã®ããã«ä½¿ãåããå¿
è¦ãããã¾ãã
è£è¶³
url_for(attachment) 㧠redirect ã§ã¯ãªã proxy ã使ãããï¼
url_for(attachment)
ã¨ããã¨ãã«çæãããURL㯠Rails 6.0 以åã¨åãããªãã¤ã¬ã¯ããããã®ã§ããããã¯äºææ§ã®è¦³ç¹ãããèªç¶ã ã¨æãã¾ãã
ãããããããã£ãé
æ
®ã¯ä¸è¦ã§ããªãã¤ã¬ã¯ãã§ã¯ãªãæ°ããªãããã·ã ã使ãããå ´åããããé½åº¦ rails_storage_proxy_url
ãã¿ã¤ãããã®ãé¢åã§ãã
ãã®ãããªå ´å config.active_storage.resolve_model_to_route
ã§è¨å®ã§ããããã«ãªã£ã¦ãã¾ãã
# config/initializers/active_storage.rb Rails.application.config.active_storage.resolve_model_to_route = :rails_storage_proxy
ãããããã¨ã«ãã£ã¦ãActiveStorageã®ã¢ãã«ï¼ ActiveStorage::Attachment
ActiveStorage::Variant
ãªã© ï¼ã®ã«ã¼ã解決ã«ã¯ rails_storage_proxy_url
ã使ãããããã«ãªããåã«æ¬¡ã®ããã«æ¸ããããã«ãªãã¾ãã
<%= url_for user.photo %> <%= image_tag user.photo %>
proxy ã® URL ãCDNçµç±ã®ãã®ã«ãããï¼
rails_storage_proxy_url
ã§ã¯URLã®ãã¹ãé¨ã¯Railsãµã¼ãã¼ã®ãã®ã«ãªãã¾ããã¤ã¾ããå段ã«ãã£ãã·ã¥ããã¯ã·ãªã©ãç¨æããªãã¨ããã¡ã¤ã«åå¾ã®åº¦ã«Railsãµã¼ãã¼ã«ã¢ã¯ã»ã¹ãã¦ãã¾ãã¾ãã
ããã§ã¯ãªããCDNãçµç±ãã¦Railsãµã¼ãã¼ã«ã¢ã¯ã»ã¹ãå¿çããã£ãã·ã¥ããããã«è¨å®ããä¸ã§ãçæããURLãCDNã®ãã®ã«ããã°ãRailsãµã¼ãã¼ã«ãããè² è·ã¯ã¯ããã«å°ãããå¿çã¯ã¯ããã«éãã§ãã¾ãã
ãã®ãããªå ´åã¯ã config.active_storage.resolve_model_to_route
ã«å ãã¦ãã¤ã¬ã¯ãã«ã¼ãã£ã³ã°æ©è½ãå©ç¨ããã¨ãã¾ãæ¸ãã¾ãã
Active storage add proxying by fleck · Pull Request #34477 · rails/rails · GitHub
# config/initializers/active_storage.rb Rails.application.config.active_storage.resolve_model_to_route = :cdn_proxy
# config/routes.rb # ï¼çç¥ï¼ direct :cdn_proxy do |model, options| cdn_options = if Rails.env.development? Rails.application.routes.default_url_options else { protocol: 'https', port: 443, host: Rails.env.production? ? "cdn.myapp.takeyuweb.co.jp" : "#{Rails.env}.cdn.myapp.takeyuweb.co.jp" } end if model.respond_to?(:signed_id) route_for( :rails_service_blob_proxy, model.signed_id, model.filename, options.merge(cdn_options) ) else signed_blob_id = model.blob.signed_id variation_key = model.variation.key filename = model.blob.filename route_for( :rails_blob_representation_proxy, signed_blob_id, variation_key, filename, options.merge(cdn_options) ) end end
ãã®ããã«ããã°ã次ã®ããã«ããã ãã§ï¼éçºã¢ã¼ã以å¤ã§ã¯ï¼CDNçµç±ã®URLã«ãªãã¾ãã
<%= image_tag user.photo %>
<img src="https://cdn.myapp.takeyuweb.co.jp/rails/active_storage/blobs/proxy/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZDg9IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d248c202e2e205d7cff8385cceaa598ea0fe244f/photo.png" />
å¿ç¨
CDK 㧠CloudFront Distribution ãä½ã£ã¦ãã®è¨å®ã¨çµã¿åãããæ¹æ³ã«ã¤ãã¦ç´¹ä»ããè¨äºããã¡ãã«ãªãã¾ãã