I'm using SFTP client for fetching files froma SFTP server. I'm able to successfully read the file and store it, but I'm unsable to delete it from SFTP server after I'm done saving it.
CODE
require 'net/sftp'
class Sftp
def self.save
Net::SFTP.start(somehost, ****, password: ****) do |sftp|
sftp.dir.foreach("/files") do |entry|
next unless entry.file?
file_name = entry.name
source_file = "/files/#{file_name}"
destination_file = "tmp/#{file_name}"
sftp.download!(source_file, destination_file)
df = File.open(destination_file, "r")
file_data = df.read
# Some logic to utilise read file info. in variable "file_data"
File.delete(df) # deleted from tmp
sftp.remove!(source_file) # deleted from sftp server
end
end
end
end
When the line sftp.remove!(source_file)
is executed, I get error like:
"Net::SFTP::StatusException (3, \"permission denied\")"
Permission for files directory
drwxr-xr-x 2 root root 4096 Dec 22 10:54 files
Permission for files inside the files directory:
drwxr-xr-x 2 root root 4096 Dec 22 10:54 .
drwxr-xr-x 4 root root 4096 Dec 18 15:29 ..
-rwxrwxrwx 1 root root 749199 Dec 18 14:39 a.pdf
-rwxrwxrwx 1 root root 7945 Dec 18 15:41 b.pdf
-rwxrwxrwx 1 root root 7945 Dec 22 10:54 c.pdf
EDIT
I replaced the following line of code
sftp.remove!(source_file)
with
sftp.send(:exec, "sudo rm /var/sftp/#{source_file}")
Now, the removal works, but only for the first file. Then the loop exits without any error.
What may be the reason?