Method: S3File.list

Defined in:
lib/s3file/ls.rb

.list(location = nil, display = nil) ⇒ Object

Gets a list of all the files in the given location in the bucket

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/s3file/ls.rb', line 21

def list(location = nil, display = nil)
  return [] if location.nil?
  raise(PathError, "Not S3 Path") unless s3?(location)
  # display can take 3 possible values "all", "dir" or "file"
  # if display is nil or any other value other than dir or file then
  # display = "all"
  # if display is 'dir' or 'file' then
  # display takes that value
  if display.nil? || !display.match(/dir|file/)
    display = "all"
  end

  directories = []
  files = []
  location += "/" if location == s3_bucket(location)
  entries = run_command("s3cmd ls #{location} -c #{@@config_file}")

  directory = s3_directory(location)
  entries.split("\n").each do |entry|
    if display.match(/all|dir/) && entry.strip.match(/DIR/)
      dir = entry.strip.sub(/DIR/, '').strip.sub("#{directory}", "")
      if dir == "/"
        directories << (location + "/")
      else
        directories << dir
      end
    elsif display.match(/all|file/) && !entry.strip.match(/DIR/)
      file = entry.strip.sub(/\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}\s*/, '').split(" ")[1].sub("#{directory}","")
      if file == ""
        files << location
      else
        files << file
      end
    end
  end
  directories + files
end