Allow Paperclip attachments to be stored on FTP servers.
Add this line to your application's Gemfile
:
gem "paperclip-storage-ftp"
And then execute:
$ bundle
Or install it manually:
$ gem install paperclip-storage-ftp
Somewhere in your code:
require "paperclip/storage/ftp"
In your model:
class User < ActiveRecord::Base
has_attached_file :avatar, {
# Choose the FTP storage backend
:storage => :ftp,
# Set where to store the file on the FTP server(s).
# This supports Paperclip::Interpolations.
:path => "/path_on_ftp_server/:attachment/:id/:style/:filename",
# The full URL of where the attachment is publicly accessible.
# This supports Paperclip::Interpolations.
:url => "/url_prefix/:attachment/:id/:style/:filename",
# The list of FTP servers to use
:ftp_servers => [
{
:host => "ftp1.example.com",
:user => "foo",
:password => "bar"
},
# Add more servers if needed
{
:host => "ftp2.example.com",
:user => "foo",
:password => "bar",
:port => 2121, # optional, 21 by default
:passive => true # optional, false by default
}
],
# Optional socket connect timeout (in seconds).
# This only limits the connection phase, once connected
# this option is of no more use.
:ftp_connect_timeout => 5, # optional, nil by default (OS default timeout)
# Optional flag to skip dead servers.
# If set to true and the connection to a particular server cannot be
# established, the connection error will be ignored and the files will
# not be uploaded to that server.
# If set to false and the connection to a particular server cannot be established,
# a SystemCallError will be raised (Errno::ETIMEDOUT, Errno::ENETUNREACH, etc.).
:ftp_ignore_failing_connections => true # optional, false by default
}
end
- Avoid unnecessary connection to server(s) when there are no files to be written or deleted #26
- Rescue FTPPermError exception during
file_exists?
#22
- Remove empty parent directories after image deletion #21
- Raise
Paperclip::Storage::Ftp::NoServerAvailable
error when using:ftp_ignore_failing_connections => true
but all servers are down - Avoid connecting to all servers for operations that just need one
- New options
:ftp_connect_timeout
and:ftp_ignore_failing_connections
. See usage example above.
Mostly performance enhancements
- Use one thread per server to upload assets to multiple servers in parallel #10
- Avoid excessive reconnects to reduce overall upload time
- Close connnection immediately after being used
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- Sebastian Röbke
- and other friendly contributors
You can find out more about our work on our dev blog.
Copyright (c) 2015 XING AG
Released under the MIT license. For full details see LICENSE included in this distribution.