Module: Commands

Includes:
S3Ranger
Defined in:
lib/s3ranger/commands.rb

Constant Summary collapse

AVAILABLE_ACLS =
[:public_read, :public_read_write, :private]
AVAILABLE_METHODS =
['read', 'get', 'put', 'write', 'delete']

Constants included from S3Ranger

S3Ranger::VERSION

Class Method Summary collapse

Methods included from S3Ranger

safe_join

Class Method Details

._cmd_createbucket(args) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/s3ranger/commands.rb', line 20

def Commands._cmd_createbucket args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]

  begin
    params = {}
    if acl = args[:options]['--acl']
      raise WrongUsage.new(nil, "Invalid ACL. Should be any of #{EXISTING_ACLS.join ', '}") if not AVAILABLE_ACLS.include? acl
      params.merge!({:acl => acl.to_sym})
    end

    args[:s3].buckets.create args[:bucket], params
  rescue AWS::S3::Errors::BucketAlreadyExists => exc
    raise FailureFeedback.new("Bucket `#{bucket}' already exists")
  end
end

._cmd_delete(args) ⇒ Object

Raises:



61
62
63
64
65
# File 'lib/s3ranger/commands.rb', line 61

def Commands._cmd_delete args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]
  raise WrongUsage.new(nil, "You need to inform a key") if not args[:key]
  args[:s3].buckets[args[:bucket]].objects[args[:key]].delete
end

._cmd_deletebucket(args) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/s3ranger/commands.rb', line 36

def Commands._cmd_deletebucket args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]

  # Getting the bucket
  bucket_obj = args[:s3].buckets[args[:bucket]]

  # Do not kill buckets with content unless explicitly asked
  if not args[:options]['--force'] and bucket_obj.objects.count > 0
    raise FailureFeedback.new("Cowardly refusing to remove non-empty bucket `#{bucket}'. Try with -f.")
  end

  begin
    bucket_obj.delete!
  rescue AWS::S3::Errors::AccessDenied => exc
    raise FailureFeedback.new("Access Denied")
  end
end

._cmd_get(args) ⇒ Object

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/s3ranger/commands.rb', line 89

def Commands._cmd_get args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]
  raise WrongUsage.new(nil, "You need to inform a key") if not args[:key]
  raise WrongUsage.new(nil, "You need to inform a file") if not args[:file]

  # Saving the content to be downloaded to the current directory if the
  # destination is a directory
  path = File.absolute_path args[:file]
  path = S3Ranger.safe_join [path, File.basename(args[:key])] if File.directory? path

  File.open(path, 'wb') do |f|
    begin
      args[:s3].buckets[args[:bucket]].objects[args[:key]].read do |chunk| f.write(chunk) end
    rescue AWS::S3::Errors::NoSuchBucket
      raise FailureFeedback.new("There's no bucket named `#{args[:bucket]}'")
    rescue AWS::S3::Errors::NoSuchKey
      raise FailureFeedback.new("There's no key named `#{args[:key]}' in the bucket `#{args[:bucket]}'")
    end
  end
end

._cmd_list(args) ⇒ Object

Raises:



54
55
56
57
58
59
# File 'lib/s3ranger/commands.rb', line 54

def Commands._cmd_list args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]
  args[:s3].buckets[args[:bucket]].objects.with_prefix(args[:key] || "").each do |object|
    puts "#{object.key}\t#{object.content_length}\t#{object.last_modified}"
  end
end

._cmd_listbuckets(args) ⇒ Object



14
15
16
17
18
# File 'lib/s3ranger/commands.rb', line 14

def Commands._cmd_listbuckets args
  args[:s3].buckets.each do |bkt|
    puts "#{bkt.name}"
  end
end

._cmd_put(args) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
# File 'lib/s3ranger/commands.rb', line 80

def Commands._cmd_put args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]
  raise WrongUsage.new(nil, "You need to inform a file") if not args[:file]

  # key + file name
  name = S3Ranger.safe_join [args[:key], File.basename(args[:file])]
  args[:s3].buckets[args[:bucket]].objects[name].write Pathname.new(args[:file])
end

._cmd_sync(args) ⇒ Object



110
111
112
113
# File 'lib/s3ranger/commands.rb', line 110

def Commands._cmd_sync args
  cmd = SyncCommand.new args, *ARGV
  cmd.run
end

._cmd_url(args) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/s3ranger/commands.rb', line 67

def Commands._cmd_url args
  raise WrongUsage.new(nil, "You need to inform a bucket") if not args[:bucket]
  raise WrongUsage.new(nil, "You need to inform a key") if not args[:key]

  method = args[:options]['--method'] || 'read'
  raise WrongUsage.new(nil, "") unless AVAILABLE_METHODS.include? method

  opts = {}
  opts.merge!({:secure => args[:options]["--no-ssl"].nil?})
  opts.merge!({:expires => args[:options]["--expires-in"]}) if args[:options]["--expires-in"]
  p (args[:s3].buckets[args[:bucket]].objects[args[:key]].url_for method.to_sym, opts).to_s
end