Class: BFS::Bucket::SFTP

Inherits:
Abstract
  • Object
show all
Defined in:
lib/bfs/bucket/sftp.rb

Overview

SFTP buckets are operating on SFTP connections.

Constant Summary collapse

StatusCodes =
Net::SFTP::Constants::StatusCodes

Instance Method Summary collapse

Constructor Details

#initialize(host, prefix: nil, **opts) ⇒ SFTP

Initializes a new bucket

Parameters:

  • host (String)

    the host name

  • opts (Hash)

    options

Options Hash (**opts):

  • :port (Integer)

    custom port. Default: 22.

  • :user (String)

    user name for login.

  • :password (String)

    password for login.

  • :prefix (String)

    optional prefix.

  • :compression (Boolean)

    use compression.

  • :keepalive (Boolean)

    use keepalive.

  • :keepalive_interval (Integer)

    interval if keepalive enabled. Default: 300.

  • :keys (Array<String>)

    an array of file names of private keys to use for publickey and hostbased authentication.

  • :verify_host_key (Symbol)

    host-key verification should be, either :never, :accept_new_or_local_tunnel, :accept_new, or :always.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bfs/bucket/sftp.rb', line 22

def initialize(host, prefix: nil, **opts)
  super(**opts)

  @prefix  = prefix
  @session = Net::SSH.start(host, nil, **opts.slice(*Net::SSH::VALID_OPTIONS), non_interactive: true)

  if @prefix # rubocop:disable Style/GuardClause
    @prefix = "#{norm_path(@prefix)}/"
    mkdir_p @prefix
  end
end

Instance Method Details

#closeObject

Closes the underlying connection



100
101
102
# File 'lib/bfs/bucket/sftp.rb', line 100

def close
  @session.close unless @session.closed?
end

#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object

Creates a new file and opens it for writing

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :encoding (String|Encoding)

    Custom file encoding.

  • :perm (Integer)

    Custom file permission, default: 0600.



67
68
69
70
71
72
73
74
75
# File 'lib/bfs/bucket/sftp.rb', line 67

def create(path, encoding: self.encoding, perm: self.perm, **opts, &block)
  full = full_path(path)

  opts[:preserve] = true if perm && !opts.key?(:preserve)
  BFS::Writer.new(path, encoding: encoding, perm: perm) do |temp_path|
    mkdir_p File.dirname(full)
    @session.sftp.upload!(temp_path, full, **opts)
  end.perform(&block)
end

#glob(pattern = '**/*', **_opts) ⇒ Object

Iterates over the contents of a bucket using a glob pattern



42
43
44
45
46
47
48
# File 'lib/bfs/bucket/sftp.rb', line 42

def glob(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern) do |path, attrs|
      acc << file_info(path, attrs)
    end
  end
end

#info(path, **_opts) ⇒ Object

Info returns the object info



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bfs/bucket/sftp.rb', line 51

def info(path, **_opts)
  full = full_path(path)
  path = norm_path(path)
  attrs = @session.sftp.stat!(full)
  raise BFS::FileNotFound, path unless attrs.file?

  file_info path, attrs
rescue Net::SFTP::StatusException => e
  raise BFS::FileNotFound, path if e.code == StatusCodes::FX_NO_SUCH_FILE

  raise
end

#ls(pattern = '**/*', **_opts) ⇒ Object

Lists the contents of a bucket using a glob pattern



35
36
37
38
39
# File 'lib/bfs/bucket/sftp.rb', line 35

def ls(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern) {|path, _| acc << path }
  end
end

#open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) ⇒ Object

Opens an existing file for reading



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bfs/bucket/sftp.rb', line 78

def open(path, encoding: self.encoding, tempdir: nil, **_opts, &block)
  full = full_path(path)
  temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding)
  temp.close

  @session.sftp.download!(full, temp.path)
  File.open(temp.path, encoding: encoding, &block)
rescue Net::SFTP::StatusException => e
  raise BFS::FileNotFound, path if e.code == StatusCodes::FX_NO_SUCH_FILE

  raise
end

#rm(path, **_opts) ⇒ Object

Deletes a file.



92
93
94
95
96
97
# File 'lib/bfs/bucket/sftp.rb', line 92

def rm(path, **_opts)
  full = full_path(path)
  @session.sftp.remove!(full)
rescue Net::SFTP::StatusException => e
  raise unless e.code == StatusCodes::FX_NO_SUCH_FILE
end