Class: BFS::Bucket::SFTP
- Inherits:
-
Abstract
- Object
- Abstract
- BFS::Bucket::SFTP
- 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
-
#close ⇒ Object
Closes the underlying connection.
-
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#glob(pattern = '**/*', **_opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern.
-
#info(path, **_opts) ⇒ Object
Info returns the object info.
-
#initialize(host, prefix: nil, **opts) ⇒ SFTP
constructor
Initializes a new bucket.
-
#ls(pattern = '**/*', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) ⇒ Object
Opens an existing file for reading.
-
#rm(path, **_opts) ⇒ Object
Deletes a file.
Constructor Details
#initialize(host, prefix: nil, **opts) ⇒ SFTP
Initializes a new bucket
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
#close ⇒ Object
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
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 |