DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Handling File Transfers in a Heroku Environment with Net::SFTP

March 20, 2025

Daily readers, today’s article is a bit different. Usually, I dive into a specific Ruby or Ruby on Rails topic, but today, time is short, and I need to handle different tasks. So, I’ll share a quick but useful real-world problem I encountered.


Need Help with a Heroku Project?

Do you need to handle a project running on a Heroku server? Contact me—I can help you:

Get in Touch


The Challenge

One time, I needed to download a file that was generated on the fly inside a Heroku server. Since Heroku’s filesystem is ephemeral, I had to find a way to retrieve the file manually. My approach was to copy the file to another FTP server using Net::SFTP.

The Solution

Below is the simple Ruby algorithm I used to upload the file:

require 'net/sftp'

def upload(file_path)
  begin
    Net::SFTP.start(ENV['host'], ENV['username'], password: ENV['password']) do |sftp|
      sftp.upload!(file_path, "./#{file_path}")
    end

    puts 'Successful'
  rescue Net::SFTP::StatusException => e
    puts "Error: #{e.message}"
  rescue Net::ReadTimeout, Net::OpenTimeout => e
    puts "Error: #{e.message}"
  end
end 
Enter fullscreen mode Exit fullscreen mode

Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>


Need Expert Ruby on Rails Developers to Elevate Your Project?


Configuration

To make this work, you need to set up environment variables in your .env file:

host=your_host_name
username=your_ftp_username
password=your_secure_password 
Enter fullscreen mode Exit fullscreen mode

If you are using a Dockerized Ruby on Rails environment (like the one discussed in this article), you just need to update your docker-compose.yml file:

rails:
  build:
    context: ..
    dockerfile: docker_environment/Dockerfile
  container_name: env-ruby_stack_news.rails
  depends_on:
    - db
    - mailcatcher
  environment:
    host=your_host_name
    username=your_ftp_username
    password=your_secure_password
  ports:
    - 3000:3000
  tty: true
  volumes:
    - ${PWD}:/app
    - ${PWD}/.root:/root
  platform: linux/amd64
Enter fullscreen mode Exit fullscreen mode

Heroku:

Set the variables in the form to add them.


Heroku


Why This Matters

Even small tasks like this can become roadblocks if you don’t have a quick solution. Handling file transfers inside Heroku (or any cloud environment) requires a workaround due to its ephemeral nature. Net::SFTP provides a simple and efficient way to move files securely.

Hope this helps! Let me know if you have a different approach or an alternative solution.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay