Class: Fog::Aliyun::Storage::Directories

Inherits:
Collection
  • Object
show all
Defined in:
lib/fog/aliyun/models/storage/directories.rb

Instance Method Summary collapse

Instance Method Details

#allObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/aliyun/models/storage/directories.rb', line 12

def all
  containers = service.get_containers
  return nil if containers.nil?
  data = []
  i = 0
  containers.each do |entry|
    key = entry['Prefix'][0]
    key[-1] = ''
    data[i] = { key: key }
    i += 1
  end

  load(data)
end

#get(key, options = {}) ⇒ Object

get method used to get a specified directory. If the directory is not exist, this method will create a new with ‘key’ In order to support multi-buckets scenario which making bucket as a solo directory, it have been expanded. If key is a directory(including /), return an existed or a new one; If key does not contain /, if bucket, return ”, else return an existed or a new one directory;



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fog/aliyun/models/storage/directories.rb', line 32

def get(key, options = {})
  if key.is_a? Array
    key = key[0]
  end
  if !key.nil? && key != '' && key != '.'
    key = key.chomp('/')
    if key.include? '/'
      dir = key + '/'
      ret = service.head_object(dir, options)
      new(key: key) if ret.data[:status] == 200
    else
      remap_attributes(options, {
          :delimiter  => 'delimiter',
          :marker     => 'marker',
          :max_keys   => 'max-keys',
          :prefix     => 'prefix'
      })
      data = service.get_bucket(key, options)
      directory = new(:key => data['Name'], :is_persisted => true)
      options = {}
      for k, v in data
        if ['CommonPrefixes', 'Delimiter', 'IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(k)
          # Sometimes, the v will be a Array, like "Name"=>["blobstore-droplet1"], "Prefix"=>[{}], "Marker"=>[{}], "MaxKeys"=>["100"], "Delimiter"=>[{}], "IsTruncated"=>["false"]
          # and there needs to parse them
          if !v.nil? && (v.is_a? Array) && (v.size > 0)
            if v[0].is_a? Hash
              v = nil
            else
              v = v[0]
            end
          end
          options[k] = v
        end
      end
      directory.files.merge_attributes(options)
      if data.key?('Contents') && !data['Contents'].nil?
        directory.files.load(data['Contents'])
      end
      directory
    end
  else
    new(key: '')
  end
rescue Fog::Aliyun::Storage::NotFound
  nil
end