File: directory.rb

package info (click to toggle)
ruby-fog-aws 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,092 kB
  • sloc: ruby: 72,795; javascript: 14; makefile: 9; sh: 4
file content (159 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'fog/aws/models/storage/files'
require 'fog/aws/models/storage/versions'

module Fog
  module AWS
    class Storage
      class Directory < Fog::Model
        VALID_ACLS = ['private', 'public-read', 'public-read-write', 'authenticated-read']

        attr_reader :acl

        identity  :key,           :aliases => ['Name', 'name']

        attribute :creation_date, :aliases => 'CreationDate', :type => 'time'

        def acl=(new_acl)
          unless VALID_ACLS.include?(new_acl)
            raise ArgumentError.new("acl must be one of [#{VALID_ACLS.join(', ')}]")
          else
            @acl = new_acl
          end
        end

        def destroy
          requires :key
          service.delete_bucket(key)
          true
        rescue Excon::Errors::NotFound
          false
        end

        # @param options [Hash] (defaults to: {}) — a customizable set of options.
        #   Consider tuning this values for big buckets.
        # @option options timeout [Integer] — default: Fog.timeout — Maximum number of
        #   seconds to wait for the bucket to be empty.
        # @option options interval [Proc|Integer] — default: Fog.interval — Seconds to wait before
        #   retrying to check if the bucket is empty.
        def destroy!(options = {})
          requires :key
          options = {
            timeout: Fog.timeout,
            interval: Fog.interval,
          }.merge(options)

          attempts = 0
          begin
            clear!
            Fog.wait_for(options[:timeout], options[:interval]) { objects_keys.size == 0 }
            service.delete_bucket(key)
            true
          rescue Excon::Errors::HTTPStatusError
            false
          end
        end

        def location
          @location ||= (bucket_location || Storage::DEFAULT_REGION)
        end

        # NOTE: you can't change the region once the bucket is created
        def location=(new_location)
          @location = new_location
        end

        def files
          @files ||= Fog::AWS::Storage::Files.new(:directory => self, :service => service)
        end

        def payer
          requires :key
          data = service.get_request_payment(key)
          data.body['Payer']
        end

        def payer=(new_payer)
          requires :key
          service.put_request_payment(key, new_payer)
          @payer = new_payer
        end

        def versioning?
          requires :key
          data = service.get_bucket_versioning(key)
          data.body['VersioningConfiguration']['Status'] == 'Enabled'
        end

        def versioning=(new_versioning)
          requires :key
          service.put_bucket_versioning(key, new_versioning ? 'Enabled' : 'Suspended')
        end

        def versions
          @versions ||= Fog::AWS::Storage::Versions.new(:directory => self, :service => service)
        end

        def public=(new_public)
          self.acl = new_public ? 'public-read' : 'private'
          new_public
        end

        def public_url
          requires :key
          if service.get_bucket_acl(key).body['AccessControlList'].find {|grant| grant['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers' && grant['Permission'] == 'READ'}
            service.request_url(
              :bucket_name => key
            )
          else
            nil
          end
        end

        def save
          requires :key

          options = {}

          options['x-amz-acl'] = acl if acl

          # http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
          # Ignore the default region us-east-1
          if !persisted? && location != DEFAULT_REGION
            options['LocationConstraint'] = location
          end

          service.put_bucket(key, options)
          attributes[:is_persisted] = true

          true
        end

        def persisted?
          # is_persisted is true in case of directories.get or after #save
          # creation_date is set in case of directories.all
          attributes[:is_persisted] || !!attributes[:creation_date]
        end

        private

        def bucket_location
          requires :key
          return nil unless persisted?
          data = service.get_bucket_location(key)
          data.body['LocationConstraint']
        end

        def objects_keys
          requires :key
          bucket_query = service.get_bucket(key)
          bucket_query.body["Contents"].map {|c| c["Key"]}
        end

        def clear!
          requires :key
          service.delete_multiple_objects(key, objects_keys) if objects_keys.size > 0
        end
      end
    end
  end
end