Class: S3Patron::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/s3_patron/cli.rb

Instance Method Summary collapse

Instance Method Details

#buckets(region = ENV['AWS_REGION']) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/s3_patron/cli.rb', line 69

def buckets(region=ENV['AWS_REGION'])
    s3 = Aws::S3::Resource.new(region: region)
    begin
        if options[:all]
            puts 'Showing all buckets.'.yellow
            s3.buckets.each do |b|
                puts b.name.to_s
            end
        else
            puts "Showing first #{options[:limit]} buckets.Use -a flag to show all listings".yellow
            s3.buckets.limit(options[:limit]).each do |b|
                puts b.name.to_s
            end
        end
    rescue
        puts 'Something went wrong. Please try again.'.red
    end
end

#create(bucket_name, region = ENV['AWS_REGION']) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/s3_patron/cli.rb', line 46

def create(bucket_name, region=ENV['AWS_REGION'])
    s3 = Aws::S3::Resource.new(region: region)
    s3.create_bucket(bucket: bucket_name.to_s)
    puts "#{bucket_name} successfully created.".yellow
rescue Aws::S3::Errors::BucketAlreadyExists
    puts 'The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.'.red
rescue Aws::S3::Errors::InvalidBucketName
    puts "The specified bucket name is not a valid format based on AWS guidelines.\nFor more info please visit http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html".red
rescue Aws::S3::Errors::BucketAlreadyOwnedByYou
  puts "There is already an existing bucket with the same name".red
end

#download(bucket_name, item, path = Dir.pwd, region = ENV['AWS_REGION']) ⇒ Object



60
61
62
63
64
# File 'lib/s3_patron/cli.rb', line 60

def download(bucket_name, item, path=Dir.pwd, region=ENV['AWS_REGION'])
  s3 = Aws::S3::Resource.new(region: region)
  obj = s3.bucket(bucket_name).object(item)
  obj.get(response_target: "#{path}/#{obj.key}")
end

#list(bucket, region = ENV['AWS_REGION']) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/s3_patron/cli.rb', line 10

def list(bucket, region=ENV['AWS_REGION'])
  s3 = Aws::S3::Resource.new(region: region)
  bucket = s3.bucket(bucket)
  begin
      if options[:all]
          puts "Showing all objects inside bucket named #{bucket.name}.".yellow
          bucket.objects.each do |item|
            puts item.key
          end
      else
          puts "Showing first #{options[:limit]} objects from #{bucket.name}.Use -a flag to show all objects".yellow
          bucket.objects.limit(options[:limit]).each do |item|
            puts item.key
          end
      end
  rescue
      puts 'Something went wrong. Please try again.'.red
  end
end

#upload(bucket_name, file, region = ENV['AWS_REGION']) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/s3_patron/cli.rb', line 31

def upload(bucket_name, file, region=ENV['AWS_REGION'])
  s3 = Aws::S3::Resource.new(region: region)
  begin
    item = file
    bucket = bucket_name
    name = File.basename(item)
    obj = s3.bucket(bucket).object(name)
    obj.upload_file(item)
    puts "#{file} successfully uploaded to #{bucket}".yellow
  rescue
    puts "Something went wrong. Please try again.".red
  end
end