2012-11-04 75 views
3

我正在開發一個平臺,允許商家上傳他們的商店圖片。我正在使用Carrierwave +霧。由於該應用程序託管在heroku上,圖片需要存儲在亞馬遜S3上。霧S3上傳導致

在我的本地機器上,如果存儲器是本地的,一切正常,但霧處理上傳時出錯。

我將首先發布本地存儲版本(正常工作),然後是我目前支持霧的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給出了Permission denied錯誤消息(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' 

這個錯誤讓我困惑,因爲在應用程序的/ 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 
+0

您可以使用[Carrierwave Direct](http://www.knight.i o/repos/dwilkie/carrierwave_direct)將圖片直接從瀏覽器上傳到S3。如果圖片上傳比Heroku允許請求在不發送響應的情況下運行時間更長,您可能會遇到問題。它也應該解決你的問題,雖然方式稍有不同。 –

回答

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 

此外,您可能有添加配置/初始化程序/ 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 {}