ruby-on-rails - aws - mount_uploader
Carrierwave gem Secure File Path no puede leer el error del archivo (2)
Según su configuración, CarrierWave debe guardar su archivo dentro de "/home/pubudu/Projects/istockseller/public/uploads/tobereviewed/thpubs/#{model.user.username.downcase}/somefile.ext"
Entonces, idealmente tu código debería ser
send_file "#{Rails.root}/public/#{file.image.url}"
Suponiendo que su columna de carga es imagen.
Traté de seguir la ''Carga segura'' en la onda portadora, lo que es un poco confuso porque he personalizado la ruta del archivo y todo un poco. Cuando intento ejecutar la aplicación, aparece el error "No se puede leer el archivo".
Aquí está la ruta:
match "/uploads/tobereviewed/:user.:username.downcase/:basename.:extension", :controller => "photos", :action => "uploaded", via: :get
El sotre_dir del cargador:
class SubmitUploader < CarrierWave::Uploader::Base
def store_dir
"uploads/tobereviewed/#{model.user.username.downcase}"
end
Inicializador carrierwave.rb:
CarrierWave.configure do |config|
config.permissions = 0600
config.directory_permissions = 0700
config.root = Rails.root
end
Controlador de fotos:
def uploaded
file = Submit.first
send_file "#{Rails.root}/uploads/tobereviewed/#{file.user.username.downcase}/#{file.id}"
end
El registro de error completo:
Started GET "/uploaded" for 127.0.0.1 at 2014-11-27 18:19:09 +0530
Processing by PhotosController#uploaded as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Submit Load (2.6ms) SELECT "submits".* FROM "submits" ORDER BY "submits"."id" ASC LIMIT 1
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Sent file /home/pubudu/Projects/istockseller/uploads/tobereviewed/thpubs/36603935 (0.7ms)
Completed 500 Internal Server Error in 54ms
ActionController::MissingFile - Cannot read file /home/pubudu/Projects/istockseller/uploads/tobereviewed/thpubs/36603935:
actionpack (4.2.0.beta4) lib/action_controller/metal/data_streaming.rb:68:in `send_file''
actionpack (4.2.0.beta4) lib/action_controller/metal/instrumentation.rb:49:in `block in send_file''
activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `block in instrument''
activesupport (4.2.0.beta4) lib/active_support/notifications/instrumenter.rb:20:in `instrument''
activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `instrument''
actionpack (4.2.0.beta4) lib/action_controller/metal/instrumentation.rb:47:in `send_file''
() home/pubudu/Projects/istockseller/app/controllers/photos_controller.rb:24:in `uploaded''
actionpack (4.2.0.beta4) lib/action_controller/metal/implicit_render.rb:4:in `send_action''
actionpack (4.2.0.beta4) lib/abstract_controller/base.rb:198:in `process_action''
actionpack (4.2.0.beta4) lib/action_controller/metal/rendering.rb:10:in `process_action''
actionpack (4.2.0.beta4) lib/abstract_controller/callbacks.rb:20:in `block in process_action''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:117:in `call''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:169:in `block in halting''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:234:in `block in halting''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:234:in `block in halting''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:169:in `block in halting''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:92:in `_run_callbacks''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:81:in `run_callbacks''
actionpack (4.2.0.beta4) lib/abstract_controller/callbacks.rb:19:in `process_action''
actionpack (4.2.0.beta4) lib/action_controller/metal/rescue.rb:29:in `process_action''
actionpack (4.2.0.beta4) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action''
activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `block in instrument''
activesupport (4.2.0.beta4) lib/active_support/notifications/instrumenter.rb:20:in `instrument''
activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `instrument''
actionpack (4.2.0.beta4) lib/action_controller/metal/instrumentation.rb:30:in `process_action''
actionpack (4.2.0.beta4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action''
activerecord (4.2.0.beta4) lib/active_record/railties/controller_runtime.rb:18:in `process_action''
actionpack (4.2.0.beta4) lib/abstract_controller/base.rb:137:in `process''
actionview (4.2.0.beta4) lib/action_view/rendering.rb:30:in `process''
actionpack (4.2.0.beta4) lib/action_controller/metal.rb:195:in `dispatch''
actionpack (4.2.0.beta4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch''
actionpack (4.2.0.beta4) lib/action_controller/metal.rb:236:in `block in action''
actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:73:in `dispatch''
actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:42:in `serve''
actionpack (4.2.0.beta4) lib/action_dispatch/journey/router.rb:43:in `block in serve''
actionpack (4.2.0.beta4) lib/action_dispatch/journey/router.rb:30:in `serve''
actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:780:in `call''
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!''
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call''
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!''
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call''
warden (1.2.3) lib/warden/manager.rb:35:in `block in call''
warden (1.2.3) lib/warden/manager.rb:34:in `call''
rack (1.6.0.beta) lib/rack/etag.rb:23:in `call''
rack (1.6.0.beta) lib/rack/conditionalget.rb:25:in `call''
rack (1.6.0.beta) lib/rack/head.rb:13:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/params_parser.rb:27:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/flash.rb:257:in `call''
rack (1.6.0.beta) lib/rack/session/abstract/id.rb:225:in `context''
rack (1.6.0.beta) lib/rack/session/abstract/id.rb:220:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/cookies.rb:558:in `call''
activerecord (4.2.0.beta4) lib/active_record/query_cache.rb:36:in `call''
activerecord (4.2.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call''
activerecord (4.2.0.beta4) lib/active_record/migration.rb:378:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:88:in `_run_callbacks''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:734:in `_run_call_callbacks''
activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:81:in `run_callbacks''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/callbacks.rb:27:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/reloader.rb:73:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call''
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call''
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call''
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call''
web-console (2.0.0.beta4) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call''
web-console (2.0.0.beta4) lib/action_dispatch/debug_exceptions.rb:13:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call''
railties (4.2.0.beta4) lib/rails/rack/logger.rb:38:in `call_app''
railties (4.2.0.beta4) lib/rails/rack/logger.rb:20:in `block in call''
activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:68:in `block in tagged''
activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:26:in `tagged''
activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:68:in `tagged''
railties (4.2.0.beta4) lib/rails/rack/logger.rb:20:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/request_id.rb:21:in `call''
rack (1.6.0.beta) lib/rack/methodoverride.rb:22:in `call''
rack (1.6.0.beta) lib/rack/runtime.rb:17:in `call''
activesupport (4.2.0.beta4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call''
rack (1.6.0.beta) lib/rack/lock.rb:17:in `call''
actionpack (4.2.0.beta4) lib/action_dispatch/middleware/static.rb:113:in `call''
rack (1.6.0.beta) lib/rack/sendfile.rb:113:in `call''
railties (4.2.0.beta4) lib/rails/engine.rb:514:in `call''
railties (4.2.0.beta4) lib/rails/application.rb:161:in `call''
rack (1.6.0.beta) lib/rack/tempfile_reaper.rb:15:in `call''
rack (1.6.0.beta) lib/rack/lint.rb:49:in `_call''
rack (1.6.0.beta) lib/rack/lint.rb:37:in `call''
rack (1.6.0.beta) lib/rack/showexceptions.rb:24:in `call''
rack (1.6.0.beta) lib/rack/commonlogger.rb:33:in `call''
rack (1.6.0.beta) lib/rack/chunked.rb:54:in `call''
rack (1.6.0.beta) lib/rack/content_length.rb:15:in `call''
rack (1.6.0.beta) lib/rack/handler/webrick.rb:89:in `service''
() home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service''
() home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run''
() home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread''
127.0.0.1 - - [27/Nov/2014:18:19:09 +0530] "GET /uploaded HTTP/1.1" 500 - 0.0743
Actualización:
Establecí la ruta de archivo correcta de esta manera:
send_file "#{file.filename.file}"
Pero luego da este error:
Cannot read file #<CarrierWave::SanitizedFile:0x007f570c2b9e58>
¿Son correctas mis rutas?
Instalar el depurador en la acción del controlador.
def uploaded
file = Submit.first
debugger
send_file "#{file.filename.file}"
end
Ahora, solicite la acción nuevamente y vaya a la consola del depurador. Un consejo aquí es abrir "irb" dentro del depurador. Simplemente escriba "irb" y presione enter.
Vea lo que está devolviendo file.filename.file. Creo que es una ruta no válida, así que verifique si el archivo existe en su sistema de archivos. Luego, puede reproducir el siguiente paso (send_file) y verificar el registro de errores.
Luego te aconsejo que abras el código fuente de la gema CarrierWave en tu computadora y encuentres qué está mal con tu código.
En su terminal, vaya a su directorio de proyectos y ejecute:
bundle show carrierwave
Esto le imprimirá la ruta de la fuente de la gema carrierwave que está utilizando. Ábralo en su editor y encuentre lo que está mal con su código, de acuerdo con el registro de errores.
Propina
Si proporciona el registro de errores actual (después de su actualización), podemos ayudarlo mejor.
Cannot read file #<CarrierWave::SanitizedFile:0x007f570c2b9e58>
no es suficiente. El backtrace completo sería mejor que esta "línea" de código (use pastebin siempre que sea posible).