Reference documentation and code samples for the Cloud Storage API class Google::Cloud::Storage::Bucket::Cors.
Bucket Cors
A special-case Array for managing the website CORS rules for a bucket.
Accessed via #cors.
Inherits
Array
Examples
require"google/cloud/storage"storage=Google::Cloud::Storage.newbucket=storage.bucket"my-bucket"bucket.corsdo|c|# Remove the last CORS rule from the arrayc.pop# Remove all existing rules with the https protocolc.delete_if{|r|r.origin.include?"http://example.com"}c.add_rule["http://example.org","https://example.org"],["GET","POST","DELETE"],headers:["X-My-Custom-Header"],max_age:3600end
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.
Parameters
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.)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Storage API - Class Google::Cloud::Storage::Bucket::Cors (v1.57.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.57.0 (latest)](/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket-Cors)\n- [1.56.0](/ruby/docs/reference/google-cloud-storage/1.56.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.55.0](/ruby/docs/reference/google-cloud-storage/1.55.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.54.0](/ruby/docs/reference/google-cloud-storage/1.54.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.53.0](/ruby/docs/reference/google-cloud-storage/1.53.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.52.0](/ruby/docs/reference/google-cloud-storage/1.52.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.51.0](/ruby/docs/reference/google-cloud-storage/1.51.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.50.0](/ruby/docs/reference/google-cloud-storage/1.50.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.49.0](/ruby/docs/reference/google-cloud-storage/1.49.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.48.1](/ruby/docs/reference/google-cloud-storage/1.48.1/Google-Cloud-Storage-Bucket-Cors)\n- [1.47.0](/ruby/docs/reference/google-cloud-storage/1.47.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.46.0](/ruby/docs/reference/google-cloud-storage/1.46.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.45.0](/ruby/docs/reference/google-cloud-storage/1.45.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.44.0](/ruby/docs/reference/google-cloud-storage/1.44.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.43.0](/ruby/docs/reference/google-cloud-storage/1.43.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.42.0](/ruby/docs/reference/google-cloud-storage/1.42.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.41.0](/ruby/docs/reference/google-cloud-storage/1.41.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.40.0](/ruby/docs/reference/google-cloud-storage/1.40.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.39.0](/ruby/docs/reference/google-cloud-storage/1.39.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.38.0](/ruby/docs/reference/google-cloud-storage/1.38.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.37.0](/ruby/docs/reference/google-cloud-storage/1.37.0/Google-Cloud-Storage-Bucket-Cors)\n- [1.36.2](/ruby/docs/reference/google-cloud-storage/1.36.2/Google-Cloud-Storage-Bucket-Cors)\n- [1.35.0](/ruby/docs/reference/google-cloud-storage/1.35.0/Google-Cloud-Storage-Bucket-Cors) \nReference documentation and code samples for the Cloud Storage API class Google::Cloud::Storage::Bucket::Cors.\n\nBucket Cors\n-----------\n\nA special-case Array for managing the website CORS rules for a bucket.\nAccessed via [#cors](/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket#Google__Cloud__Storage__Bucket_cors_instance_ \"Google::Cloud::Storage::Bucket#cors (method)\"). \n\nInherits\n--------\n\n- Array\n\nExamples\n--------\n\n```ruby\nrequire \"google/cloud/storage\"\n\nstorage = Google::Cloud::Storage.new\n\nbucket = storage.bucket \"my-bucket\"\n\nbucket.cors do |c|\n # Remove the last CORS rule from the array\n c.pop\n # Remove all existing rules with the https protocol\n c.delete_if { |r| r.origin.include? \"http://example.com\" }\n c.add_rule [\"http://example.org\", \"https://example.org\"],\n [\"GET\", \"POST\", \"DELETE\"],\n headers: [\"X-My-Custom-Header\"],\n max_age: 3600\nend\n```\n\nRetrieving the bucket's CORS rules. \n\n```ruby\nrequire \"google/cloud/storage\"\n\nstorage = Google::Cloud::Storage.new\n\nbucket = storage.bucket \"my-bucket\"\nbucket.cors.size #=\u003e 2\nrule = bucket.cors.first\nrule.origin #=\u003e [\"http://example.org\"]\nrule.methods #=\u003e [\"GET\",\"POST\",\"DELETE\"]\nrule.headers #=\u003e [\"X-My-Custom-Header\"]\nrule.max_age #=\u003e 3600\n```\n\nMethods\n-------\n\n### #add_rule\n\n def add_rule(origin, methods, headers: nil, max_age: nil)\n\nAdd a CORS rule to the CORS rules for a bucket. Accepts options for\nsetting preflight response headers. Preflight requests and responses\nare required if the request method and headers are not both [simple\nmethods](http://www.w3.org/TR/cors/#simple-method) and [simple\nheaders](http://www.w3.org/TR/cors/#simple-header). \n**Parameters**\n\n- **origin** (String, Array\\\u003cString\\\u003e) --- The [origin](http://tools.ietf.org/html/rfc6454) or origins permitted for cross origin resource sharing with the bucket. Note: \"\\*\" is permitted in the list of origins, and means \"any Origin\".\n- **methods** (String, Array\\\u003cString\\\u003e) --- 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\".\n- **headers** (String, Array\\\u003cString\\\u003e) *(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.\n- **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.)\n**Example** \n\n```ruby\nrequire \"google/cloud/storage\"\n\nstorage = Google::Cloud::Storage.new\n\nbucket = storage.create_bucket \"my-bucket\" do |b|\n b.cors.add_rule [\"http://example.org\", \"https://example.org\"],\n \"*\",\n headers: [\"X-My-Custom-Header\"],\n max_age: 300\nend\n```"]]