3

私は、マーチャントが自分の店の写真をアップロードできるようにするプラットフォームを開発しています。これにはCarrierwave+fogを使用しています。アプリはherokuでホストされているため、写真はAmazons3に保存する必要があります。

私のローカルマシンでは、ストレージがローカルであればすべてが正常に機能しますが、フォグがアップロードを処理すると問題が発生します。

最初にローカルストレージバージョンを投稿し(正常に動作します)、次に現在のフォグ対応のs3バージョンと発生しているエラーを投稿します。

ローカルストレージバージョン(正常に動作):

# app/uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

フォグ対応のs3バージョン(機能しません):

# app/uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
  storage :fog
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

# config/initializers/fog.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',       # required
    :aws_access_key_id      => 'xxxx',       # required
    :aws_secret_access_key  => 'xxxx'       # required
  }
  config.fog_directory  = 'yyyy'                     # required
  config.fog_public     = true                          # optional, defaults to true
end

新しいストアを作成または編集すると、画像は実際にはs3に正常にアップロードされますが、railsはアクセスが拒否されましたというエラーメッセージを表示します(app / tmp /carrierwave/20121104-0929-5512-8270/04.pngにアクセスできませんでした) 。

完全なエラーメッセージ:

C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1406:in `unlink'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1406:in `block in remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1414:in `platform_support'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1405:in `remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:785:in `remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:563:in `block in rm'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:562:in `each'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:562:in `rm'
carrierwave (0.5.8) lib/carrierwave/sanitized_file.rb:217:in `delete'
carrierwave (0.5.8) lib/carrierwave/uploader/store.rb:60:in `block in store!'
carrierwave (0.5.8) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.5.8) lib/carrierwave/uploader/store.rb:58:in `store!'
carrierwave (0.5.8) lib/carrierwave/mount.rb:347:in `store!'
carrierwave (0.5.8) lib/carrierwave/mount.rb:217:in `store_bild!'
activesupport (3.1.0) lib/active_support/callbacks.rb:401:in `_run_save_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.1.0) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.1.0) lib/active_record/persistence.rb:37:in `save'
activerecord (3.1.0) lib/active_record/validations.rb:50:in `save'
activerecord (3.1.0) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.1.0) lib/active_record/transactions.rb:241:in `block (2 levels) in save'
activerecord (3.1.0) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/transactions.rb:241:in `block in save'
activerecord (3.1.0) lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
activerecord (3.1.0) lib/active_record/transactions.rb:240:in `save'
activerecord (3.1.0) lib/active_record/persistence.rb:163:in `block in update_attributes'
activerecord (3.1.0) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/persistence.rb:161:in `update_attributes'
app/controllers/stores_controller.rb:62:in `block in update'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:269:in `call'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:269:in `retrieve_response_from_mimes'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/stores_controller.rb:61:in `update'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.0) lib/active_support/callbacks.rb:416:in `_run__16358980__process_action__192134115__callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:531:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/cookies.rb:326:in `call'
activerecord (3.1.0) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.0) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.0) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.0) lib/rails/engine.rb:455:in `call'
railties (3.1.0) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.0) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

app / tmp /carrierwave / 20121104-0929-5512-8270 / 04.pngをブラウザに直接入力すると正常に機能するため、このエラーは私を混乱させます。

完全を期すために、ここにコントローラーとモデルがあります(両方のバージョンで同じ)

#stores_controller.rb
def create
  @store = Store.new(params[:store])

  respond_to do |format|
    if @store.save
      format.html { redirect_to @store, notice: 'Store was successfully created.' }
      format.json { render json: @store, status: :created, location: @store }
    else
      format.html { render action: "new" }
      format.json { render json: @store.errors, status: :unprocessable_entity }
    end
  end
end

#models/store.rb
class Store < ActiveRecord::Base
  mount_uploader :bild, PictureUploader
end
4

1 に答える 1

0

アップローダに cache_dir を追加してみてください:

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

また、config/initializers/carrierwave.rb を追加する必要があるかもしれません:

CarrierWave.configure do |config|
    # hack fix for windows machine due to tmp file permission error
    # per https://github.com/jnicklas/carrierwave/issues/220/
    if ENV['RAILS_ENV'] != 'production'
      config.delete_tmp_file_after_storage = false  
    end
    config.fog_credentials = {
      :provider               => 'AWS',       # required
      :aws_access_key_id      => ENV['MF_S3_KEY'],       # required
      :aws_secret_access_key  => ENV['MF_S3_SECRET'],       # required
      :region                 => 'us-east-1'  # optional, defaults to 'us-east-1'
    }
    config.fog_directory  = ENV['MF_FLYER_BUCKET']                     # required
    #config.fog_host       = 'https://assets.example.com'            # optional, defaults to nil
    #config.fog_public     = false                                   # optional, defaults to true
    #config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
于 2012-11-14T00:29:45.270 に答える