dremme
dremme

Reputation: 769

Error when uploading file using Rails gem Net::SFTP: no such file

I am trying to upload a file to a server using the Net::SFTP gem. My code is as follows:

remote_path = "path/of/remote/file.txt"
local_path  = "path/to/local/file.txt"
Net::SFTP.start("SERVER", "USER", :password => "PASSWORD") do |sftp|
  sftp.upload!(local_path, remote_path)
end

When I execute this I get:

Net::SFTP::StatusException (Net::SFTP::StatusException open path/of/remote/file.txt (2, "no such file"))

I think the issue is that the entire remote_path of the directories doesn't exist yet. If I give it a path of directories that do exist, it will create the directories. I want the upload to also create the directories in the path if they don't exist yet. Is this indeed the issue, and if so, how can I create the directories with SFTP?

Upvotes: 0

Views: 4738

Answers (2)

Eric H.
Eric H.

Reputation: 7014

Here's a little snippet to check if the dir exists and create it if not:

if !sftp.dir.entries("root").map { |entry| entry.name }.include?("new_dir")
  sftp.mkdir("root/new_dir")
end

Upvotes: 3

phoet
phoet

Reputation: 18835

you will have to create the directory first

  sftp.mkdir! "/path/to/directory"

Upvotes: 3

Related Questions