Class: S3Ranger::CLI::Get

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

Instance Method Summary collapse

Methods inherited from BaseCmd

#has_options?, #has_prefix?, #usage

Constructor Details

#initializeGet

Returns a new instance of Get.



298
299
300
301
302
# File 'lib/s3ranger/cli.rb', line 298

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

Instance Method Details

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

Raises:



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/s3ranger/cli.rb', line 304

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