Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip files get deleted after each deploy

I use the Paperclip gem to store pictures, and on localhost it works perfectly. However, any pictures I add to my live app get deleted after every deploy.

I use Git to deploy. Here's my deployment process:

$ bundle exec cap production deploy
$ ssh [email protected]
$ chmod -R 777 /rails_apps/app/releases
$ cd /rails_apps/app/current
$ cp config/database.yml.sample config/database.yml
$ RAILS_ENV=production bundle exec rake assets:precompile
$ /etc/init.d/apache2 restart

Has anyone else run into something like this?


UPDATE:

This is not a duplicate, because the answer to this question, which is to add this line to my deploy.rb:

set :linked_dirs, fetch(:linked_dirs, []).push('public/system')

causes Paperclip to break entirely. Previously I had had an issue with not having permission to add images with Paperclip, resulting in this error:

Errno::EACCES in UsersController#update
Permission denied - /rails_apps/website/releases/20150807211111/public/system/users/avatars/000/000/562

But running this command on my server fixes the permissions:

chmod -R 777 /rails_apps/website/releases

However, modifying my deploy.rb file as shown above, causes the chmod -R 777 command to no longer work, and I once again don't have permission to add images, resulting in the same "Permission denied" error.

So that question does not supply a valid solution to my problem.

like image 500
Joe Morano Avatar asked Dec 25 '22 15:12

Joe Morano


2 Answers

best way to store your images is a place like SWS Secure, Durable & Highly-Scalable Object Storage

to set this up is really simple

# Gemfile
gem 'paperclip'
gem 'aws-sdk 

in your config/environments/production.rb

# config/environments/production.rb
config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

If you are having issues uploading images please read the following two configuration sections.

If you continue to have issues please see the Paperclip documentation page for detailed configuration options.

To override the default URL structure and place the bucket’s name “domain-style” in the URL (e.g. your_bucket_name.s3.amazonaws.com). These options can be placed in the paperclip_defaults configuration hash shown above, or into an initializer.

#config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'

I hope that this helps.

Happy Hacking

like image 73
MZaragoza Avatar answered Dec 28 '22 09:12

MZaragoza


What you do here:

set :linked_dirs, fetch(:linked_dirs, []).push('public/system')

is actually symlinking your "public/system" folder from /rails_apps/website/releases/20150807211111/public/system to /rails_apps/website/shared/public/system , so that the pictures are always stored in the shared directory, and not lost on deploy. So what you should actually do is set the proper rights for the shared folder.

like image 30
smallbutton Avatar answered Dec 28 '22 09:12

smallbutton