Class: AWS::S3::CORSRuleCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Collection::Simple
Defined in:
lib/aws/s3/cors_rule_collection.rb

Overview

Manages the CORS rules for a single bucket.

Getting Rules

To get the CORS rules for a bucket, use the Bucket#cors method. This returns a CORSRuleCollection for the bucket. The collection is enumerable.

# enumerating all rules for a buck
bucket.cors.each do |rule|
  # rule is a CORSRule object
end

Setting Rules

You can set the rules for a bucket (replacing all existing rules) using the #set method.

# accepts a list of one or more rules
bucket.rules.set(rule1, rule2)

# rules can be an array of rules
bucket.rules.set(rules)

# passing an empty list or array removes all rules
bucket.rules.set([])
bucket.rules.clear # does the same thing

Each rule can be a Hash, a CORSRule or another CORSRuleCollection. See AWS::S3::Client#put_bucket_cors for a list of keys for a rule hash.

Adding Rules

Adding rules is the same as setting rules. Rules you add will be appended to the end of the existing list of rules.

# add one or more rules
bucket.rules.add(rules)

Deleting Rules

To remove a rule, use the #delete_if method.

# delete rules that allow access from any domain
bucket.cors.delete_if{|rule| rule.allowed_origins.include?('*')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core::Collection

#each, #each_batch, #enum, #first, #in_groups_of, #page

Constructor Details

#initialize(bucket, options = {}) ⇒ CORSRuleCollection

Returns a new instance of CORSRuleCollection.

Parameters:

  • bucket (Bucket)
  • options (Hash) (defaults to: {})


69
70
71
72
# File 'lib/aws/s3/cors_rule_collection.rb', line 69

def initialize bucket, options = {}
  @bucket = bucket
  super
end

Instance Attribute Details

#bucketBucket (readonly)

Returns:



75
76
77
# File 'lib/aws/s3/cors_rule_collection.rb', line 75

def bucket
  @bucket
end

Instance Method Details

#add(*rules) ⇒ nil Also known as: create

Add one or more CORS rules to this bucket.

# adding a single rule as a hash
bucket.cors.add(
  :allowed_methods => %w(GET HEAD),
  :allowed_origins => %w(*),
  :max_age_seconds => 3600)

You can add multiple rules in a single call:

# each rule may be a hash, CORSRule or a CORSRuleCollection,
bucket.cors.add(rules)

# alternatively you can pass a list of rules
bucket.cors.add(rule1, rule2, ...)

Parameters:

  • rules (Hash, CORSRule, CORSRuleCollection)

    A list or array of one or more rules to set. Each rule may be a Hash, a CORSRule or a CORSRuleCollection.

Returns:

  • (nil)


132
133
134
# File 'lib/aws/s3/cors_rule_collection.rb', line 132

def add *rules
  self.set(self, *rules)
end

#clearnil

Removes all CORS rules attached to this bucket.

bucket.cors.count #=> 3
bucket.cors.clear
bucket.cors.count #=> 0

Returns:

  • (nil)


161
162
163
164
# File 'lib/aws/s3/cors_rule_collection.rb', line 161

def clear
  client.delete_bucket_cors(:bucket_name => bucket.name)
  nil
end

#delete_if {|rule| ... } ⇒ nil

Deletes every rule for which the block evaluates to true.

# remove all rules that are open to the 'world'
bucket.cors.delete_if{|rule| rule.allowed_origins.include?('*') }

Yields:

  • (rule)

Yield Parameters:

Yield Returns:

  • (Boolean)

    Return true for each rule you want to delete.

Returns:

  • (nil)


146
147
148
149
150
151
152
# File 'lib/aws/s3/cors_rule_collection.rb', line 146

def delete_if &block
  rules = []
  self.each do |rule|
    rules << rule unless yield(rule)
  end
  self.set(*rules)
end

#set(*rules) ⇒ nil

Replaces the CORS rules attached to this bucket. You can pass one or more rules as an array or a list.

# replace all exisitng rules with a single rule
bucket.cors.set(
  :allowed_methods => %w(GET),
  :allowed_origins => %w(http://*.mydomain.com),
  :max_age_seconds => 3600)

If you pass an empty array, all of the rules will be removed from the bucket.

# these two lines are equivilent
bucket.cors.clear
bucket.cors.set([])

Parameters:

  • rules (Hash, CORSRule, CORSRuleCollection)

    A list or array of one or more rules to set. Each rule may be a Hash, a CORSRule or a CORSRuleCollection.

Returns:

  • (nil)

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aws/s3/cors_rule_collection.rb', line 99

def set *rules

  raise ArgumentError, 'expected one or more rules' if rules.empty?

  if rules == [[]]
    self.clear
  else
    rules = rule_hashes(rules)
    client.put_bucket_cors(:bucket_name => bucket.name, :rules => rules)
  end

  nil

end