Class: SftpService

Inherits:
Object
  • Object
show all
Defined in:
app/services/sftp_service.rb

Overview

this method is for connect to remote using sftp we have 2 method, using passowrd and using key ssh id_rsa sftp.connect

Instance Method Summary collapse

Constructor Details

#initialize(host, user, key, port, password = nil) ⇒ SftpService

Returns a new instance of SftpService.



8
9
10
11
12
13
14
# File 'app/services/sftp_service.rb', line 8

def initialize(host, user, key, port, password = nil)
  @host = host
  @user = user
  @password = password
  @key = key
  @port = port
end

Instance Method Details

#connectObject

connect using passowrd



17
18
19
20
21
22
# File 'app/services/sftp_service.rb', line 17

def connect
  sftp_client.connect!
rescue StandardError, RuntimeError => e
  APMErrorHandler.report e
  raise e
end

#connect_key_onlyObject

connect using ssh key id_rsa



25
26
27
28
29
30
# File 'app/services/sftp_service.rb', line 25

def connect_key_only
  sftp_client.connect!
rescue StandardError, RuntimeError => e
  APMErrorHandler.report e
  raise e
end

#disconnectObject

disconnect sftp.disconnect



34
35
36
# File 'app/services/sftp_service.rb', line 34

def disconnect
  sftp_client.close_channel
end

#download_file(remote_path, local_path) ⇒ Object

download files sftp.download_file('/path/to/remote', '/path/to/local') sftp.download_file('/path/to/remote', '/path/to/local')



71
72
73
# File 'app/services/sftp_service.rb', line 71

def download_file(remote_path, local_path)
  sftp_client.download!(remote_path, local_path)
end

#list_files(remote_path) ⇒ Object

list files in directory sftp.list_files('/path/to/remote')



77
78
79
80
81
# File 'app/services/sftp_service.rb', line 77

def list_files(remote_path)
  sftp_client.dir.foreach(remote_path) do |entry|
    Rails.logger.debug entry.longname
  end
end

#sftp_clientObject



83
84
85
# File 'app/services/sftp_service.rb', line 83

def sftp_client
  @sftp_client ||= Net::SFTP.start(@host, @user, port: @port, key_data: [], keys: @key, keys_only: true)
end

#upload_file(local_path) ⇒ Object

upload files sftp.upload_file('/path/to/local', '/path/to/remote')



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/sftp_service.rb', line 40

def upload_file(local_path)
  sftp_client.upload!(local_path) do |event, _uploader, *args|
    case event
    when :open
      # args[0] : file metadata
      Rails.logger.error "RWG - starting upload: #{args[0].local} -> #{args[0].remote} (#{args[0].size} bytes}"
    when :put
      # args[0] : file metadata
      # args[1] : byte offset in remote file
      # args[2] : data being written (as string)
      Rails.logger.error "RWG - writing #{args[2].length} bytes to #{args[0].remote} starting at #{args[1]}"
    when :close
      # args[0] : file metadata
      Rails.logger.error "RWG - finished with #{args[0].remote}"
    when :mkdir
      # args[0] : remote path name
      Rails.logger.error "RWG - creating directory #{args[0]}"
    when :finish
      Rails.logger.error 'RWG - all done!'
    else
      raise NotImplementedError
    end
  end
rescue StandardError, RuntimeError => e
  APMErrorHandler.report e
  raise e
end