SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.

Upload big files with Capistrano

Posted on June 10th, 2007 by Alex Gorbatchev. In Capistrano. 2 comments!

Guy Naor has a nice tip to help with large file uploading in Capistrano.

class Capistrano::Actor

  # A saner put replacement that doesn't read the whole file into memory...
  def put_file(path, remote_path, options = {})
    raise "put_file can only be used with SFTP" unless Capistrano::SFTP && options.fetch(:sftp, true)

    execute_on_servers(options) do |servers|
      servers.each do |server|
        logger.info "uploading #{File.basename(path)} to #{server}"
        sftp = sessions[server].sftp
        sftp.connect unless sftp.state == :o pen
        sftp.put_file path, remote_path
        logger.debug "done uploading #{File.basename(path)} to #{server}"
      end
    end
  end

end

and I had to update it for Capistrano 2.0:

module Capistrano
  class Configuration
    module Actions
      module FileTransfer

        # A saner put replacement that doesn't read the whole file into memory...
        def put_file(path, remote_path, options = {})
          execute_on_servers(options) do |servers|
            servers.each do |server|
              logger.info "uploading #{File.basename(path)} to #{server}"
              sftp = sessions[server].sftp
              sftp.connect unless sftp.state == :o pen
              sftp.put_file path, remote_path
              logger.debug "done uploading #{File.basename(path)} to #{server}"
            end
          end
        end

      end
    end
  end
end
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 comments.

  1. jeremy

    I had a look in the sftp gem’s source:

    def put_file( local_path, remote_path )
    contents = File.open( local_path, “rb” ) { |f| f.read }
    open_handle( remote_path, “w” ) { |handle| write( handle, contents ) }
    end

    damned if you do, damned if you don’t…

  2. capistrano/transfer has a method called upload ( at least in cap 2.5 ) that does the job well

    Cheers
    Carl.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> , rel="nofollow" in use - no link dropping, no keywords or domains as names; do not spam, and do not advertise!

home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr