Class: S3Ranger::CLI::Get

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/s3ranger/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeGet

Returns a new instance of Get.



209
210
211
212
# File 'lib/s3ranger/cli.rb', line 209

def initialize
  super 'get', false, false
  @short_desc = "Retrieve an object and save to the specified file"
end

Instance Method Details

#run(s3, bucket, key, file, args) ⇒ Object

Raises:



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/s3ranger/cli.rb', line 214

def run s3, bucket, key, file, args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not bucket
  raise WrongUsage.new(nil, "You need to inform a key") if not key
  raise WrongUsage.new(nil, "You need to inform a file") if not file

  # Saving the content to be downloaded to the current directory if the
  # destination is a directory
  path = File.absolute_path file
  path = S3Ranger.safe_join [path, File.basename(key)] if File.directory? path
  File.open(path, 'wb') do |f|
    s3.buckets[bucket].objects[key].read do |chunk| f.write(chunk) end
  end
end