"permission denied" when uploading a file with Rails SFTP
Asked Answered
Q

1

5

I am working on an integration that requires my Rails 4 app to push a CSV to an SFTP server daily. The app will eventually live on Heroku.

I am using the 'net-sftp' gem.

Moving the file via FTP client (FileZilla) works.

Here's the very simple class I'm trying to use:

require 'net/ssh'
require 'net/sftp'

class SFTP

 def initialize(org)
    case org
    when 'client'
        @host = ENV['HOST']
        @username = ENV['USERNAME']
        @password = ENV['PASSWORD']
        @port = ENV['PORT']
    end
 end

 def send(local_path)
    Net::SFTP.start(@host, @username , { password: @password, port: @port } ) do |sftp|
        sftp.upload!(local_path, "/files")
    end
 end

end

Here is the error I get:

Net::SFTP::StatusException: Net::SFTP::StatusException open /files (3, "permission denied")

/net-sftp-2.1.2/lib/net/sftp/operations/upload.rb:321:in `on_open'
/net-sftp-2.1.2/lib/net/sftp/request.rb:87:in `call'
/net-sftp-2.1.2/lib/net/sftp/request.rb:87:in `respond_to'
/net-sftp-2.1.2/lib/net/sftp/session.rb:948:in `dispatch_request'
/net-sftp-2.1.2/lib/net/sftp/session.rb:911:in `when_channel_polled'

I've also tried '~/', '~' and '~/files' as the remote path, all of which I have access too.

Quadriplegia answered 27/3, 2015 at 15:17 Comment(0)
F
7

While it's not clear from the documentation, all the examples for net-sftp and even the source code suggest that the remote path must be a full path to the remote file, not just a path to a folder to upload the file to. I.e. /files/uploaded_file.txt, not just /files.

What probably happens is that the server tries to open the folder (/ or /files) as a file for writing, what fails.

Fiche answered 27/3, 2015 at 15:58 Comment(1)
Worked Wonders, Thanks @MartinMancini

© 2022 - 2024 — McMap. All rights reserved.