rails tutorial に沿ってs3の設定をし、4枚の画像をアップする機能をつくりました。
pcではおよそ6秒、スマホでは15秒かかります(開発環境cloud9では3秒)。
本番環境はherokuを使っています。
対策としては、s3のリージョンをバージニア北部にし、また、画像のバリデーションをしています。
以下に、picture_uploader.rb、micropost.rb、microposts/new.html.erb(画像アップ部のみ)の順でコードを示します。
アップ時間が遅い原因、もしくは原因を探る方法を教えていただきたいです。
uploader
1class PicturesUploader < CarrierWave::Uploader::Base 2 include CarrierWave::MiniMagick 3 process resize_to_limit: [500, 500] 4 process :fix_exif_rotation 5 6 if Rails.env.production? 7 storage :fog 8 else 9 storage :file 10 end 11 12 def store_dir 13 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 14 end 15 16 def extension_whitelist 17 %w(jpg jpeg gif png) 18 end 19 20end 21
micropost
1class Micropost < ApplicationRecord 2 mount_uploader :picture, PicturesUploader 3 mount_uploader :picture_1, PicturesUploader 4 mount_uploader :picture_2, PicturesUploader 5 mount_uploader :picture_3, PicturesUploader 6 7 private 8 # アップロードされた画像のサイズをバリデーションする 9 def picture_size 10 if picture.size > 5.megabytes 11 errors.add(:picture, "should be less than 5MB") 12 end 13 end 14end
new
1<%= f.label :picture %> 2<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/01 05:25