Anyfetch provides abstraction layer around multiple protocols to allow accessing files in uniform way.

Current support:

  • local files (via path or file:/// protocol)
  • HTTP(s)
  • FTP
  • SFTP


Add this line to your application's Gemfile:

gem 'anyfetch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install anyfetch


Simply provide file's URI. Optionally an options hash can be provided as second argument, it will be passed "as is" to specific handler (see below)., options)

Local files

Both path and file:// protocol are accepted."/path/to/file")"file:///path/to/file")


Internally open-uri (with extensions provided by open_uri_redirections gem) is used to fetch the file from HTTP and FTP servers. Feel free to pass any options recognized by open-uri or open_uri_redirections."")"", { 'User-Agent' => '...' })"")

For SFTP, net-ssh is used under the hood, user and password can be provided as part of the URI or as options. Other methods of authentication are not supported yet."s")"s", user: "user", password: "password")

Original filenames

It is not always possible to know the original filename and/or content type of the accessed file upfront, e.g. when fetching files from the URLs like or when the file is streamed from the server. Anyfetch provides original_filename method to the file instance to handle this. Internally it checks content-disposition meta information returned by the server, file's content type or simply file's basename for local files. This method can also be handy when trying to assign the file to an uploader like Paperclip or Carrierwave.

file ="")
# => filename.html


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Found a bug? File an issue on Github. Got a patch? Submit a pull request.


Bug reports and pull requests are welcome on GitHub at


The gem is available as open source under the terms of the MIT License.