Paperclip files get deleted after each deploy

我怕爱的太早我们不能终老 提交于 2019-11-30 15:26:22

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.

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

I'm assuming you are using capistrano, what about setting up shared children like:

#config/deploy.rb
set :shared_children, shared_children + %w{public/uploads}

And then just tell to capistrano to set up the shared folders properly bu running:

cap deploy:setup

Source

You may be hosting your app on a stack that does not allow you to store files directly to the filesystem at runtime.

Services such as Heroku will only allow you to temporarily store files to the system in memory, any updates, restarts, redeploys will clearly wipe this out. I'm sure this is the case for many other services out there as well.

If using such a service, you need to store your dynamic assets on a separate service such as AWS-S3 (Amazon's Simple Storage Service). You store the files there, and the information such as the url's to these assets in your databse


If you identify where you are hosting your app, I can be more detailed in the solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!