简体   繁体   中英

"permission denied" when uploading a file with Rails SFTP

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.

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. Ie /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM