Class: Google::Cloud::Storage::Bucket::Cors

  • Object
show all
Defined in:


Bucket Cors

A special-case Array for managing the website CORS rules for a bucket. Accessed via #cors.


require "google/cloud/storage"

storage =

bucket = storage.bucket "my-bucket"

bucket.cors do |c|
  # Remove the last CORS rule from the array
  # Remove all existing rules with the https protocol
  c.delete_if { |r| r.origin.include? "" }
  c.add_rule ["", ""],
             ["GET", "POST", "DELETE"],
             headers: ["X-My-Custom-Header"],
             max_age: 3600

Retrieving the bucket's CORS rules.

require "google/cloud/storage"

storage =

bucket = storage.bucket "my-bucket"
bucket.cors.size #=> 2
rule = bucket.cors.first
rule.origin #=> [""]
rule.methods #=> ["GET","POST","DELETE"]
rule.headers #=> ["X-My-Custom-Header"]
rule.max_age #=> 3600

See Also:

Defined Under Namespace

Classes: Rule

Instance Method Summary collapse

Instance Method Details

#add_rule(origin, methods, headers: nil, max_age: nil) ⇒ Object

Add a CORS rule to the CORS rules for a bucket. Accepts options for setting preflight response headers. Preflight requests and responses are required if the request method and headers are not both simple methods and simple headers.


require "google/cloud/storage"

storage =

bucket = storage.create_bucket "my-bucket" do |b|
  b.cors.add_rule ["", ""],
                  headers: ["X-My-Custom-Header"],
                  max_age: 300


  • origin (String, Array<String>)

    The origin or origins permitted for cross origin resource sharing with the bucket. Note: "*" is permitted in the list of origins, and means "any Origin".

  • methods (String, Array<String>)

    The list of HTTP methods permitted in cross origin resource sharing with the bucket. (GET, OPTIONS, POST, etc) Note: "*" is permitted in the list of methods, and means "any method".

  • headers (String, Array<String>) (defaults to: nil)

    The list of header field names to send in the Access-Control-Allow-Headers header in the preflight response. Indicates the custom request headers that may be used in the actual request.

  • max_age (Integer) (defaults to: nil)

    The value to send in the Access-Control-Max-Age header in the preflight response. Indicates how many seconds the results of a preflight request can be cached in a preflight result cache. The default value is 1800 (30 minutes.)

# File 'lib/google/cloud/storage/bucket/cors.rb', line 113

def add_rule origin, methods, headers: nil, max_age: nil
  push, methods, headers: headers, max_age: max_age)