I just deployed a Rails app on a Digital Ocean VPS (Ubuntu 14.04 Trusty Tahr)
Everything works, except for uploading images to Amazon AWS
I’m using Carrierwave to uploade images, my initializer configuration looks like this:
CarrierWave.configure do |config|
if Rails.env.staging? || Rails.env.production?
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => Rails.application.secrets.S3_KEY, # required
:aws_secret_access_key => Rails.application.secrets.S3_SECRET, # required
}
config.fog_directory = 'beautysalon2' # required
elsif Rails.env.test?
config.storage = :file
config.enable_processing = false
# make sure our uploader is auto-loaded
PromotionImageUploader
# use different dirs when testing
CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
def cache_dir
"#{Rails.root}/spec/support/uploads/tmp"
end
def store_dir
"#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
else
config.storage = :file
config.enable_processing = Rails.env.production?
end
end
I suspect there must be an issue with the AWS credentials but I’m not completely sure. I checked the /var/log/nginx/error.log on the production server, but I can’t find any errors there pointing to an issue with the AWS credentials.
Is there any other place I can look for errors on the productions server?
I think there are three places you should try looking/trying that aren’t the nginx logs:
Can you upload a file from the filesystem (such as your app repo’s README) using the console in production?
Can you temporarily download your production credentials to development and debug locally?
(for use in conjunction with the above two): I would look in the development.log and production.log files rather than the nginx logs for exception reports.
ActionController::RoutingError (No route matches [GET] "/images/environ-peeling-kuur.jpg"):
has anything to do with the issue of not being able to upload an image to Amazon S3 with Carrierwave. I think it’s just caused by the fact that there are some broken image paths on the homepage (the site is still in testing phase).
I followed your suggestion and tried out my AWS credentials in development mode. That lead me to the discovery of an issue in the carrierwave uploader class (app/uploaders/promotion_image_uploader.rb), where I called the scale() method, while the scale method was still commented out.
However, I did not solve the issue I have when uploading an image in production. Also when testing the Carrierwave upload functionality in development mode, I noticed that images are not stored in my Amazon S3 bucket.
So the problem must have to do something with my AWS credentials I guess. The tough part is that in production I don’t seem to find any debugging information.
On your development machine first make sure taht carrierwave is ifact using S3. In rails console access Rails.application.secrets.S3_ variables and make sure they are available and match your credentials. If everything is in place then I believe you should see some sort of error message in your development machine…
Thanks for your suggestion. I checked my S3 variables in the Rails console with:
Rails.application.secrets.S3_KEY
and
Rails.application.secrets.S3_SECRET
and that’s ok
I finally solved the issue. I was right about calling the scale method in my Carrierwave uploader file, as the production.log gave this error:
I, [2014-11-08T05:31:52.761109 #4424] INFO -- : Completed 500 Internal Server Error in 5ms
F, [2014-11-08T05:31:52.762952 #4424] FATAL -- :
NoMethodError (undefined method `scale' for #<PromotionImageUploader:0x007fcf33fa54d8>):
app/controllers/admin/promotions_controller.rb:15:in `create'
However fixing this issue and redeploying the application didn’t solve the issue and to my puzzlement I kept seeing the same error message in the production.log
After lots of head scratching and server restarts and redeployments, I appeared that the problem was caused by Capistrano not deploying from the remote branch.
I use the technique for deploying from a remote branch as explained in this blog post:
BRANCH=my-remote-branch bundle exec cap production deploy
I took me a while to figure this out. After I merged my feature branch to master and deployed from the master branch, al problems where solved, and now I can upload images on my production server
AWS has a somewhat complicated permissioning system, it may be that your access key ID isn’t allowed to save files in that bucket. In the AWS management console, I’d look at the user’s privileges and the bucket’s privileges.