[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Cross-origin resource sharing (CORS)\n\n[Setup](/storage/docs/using-cors)\n[Configuration samples](/storage/docs/cors-configurations)\n\nThe [same-origin policy](https://www.w3.org/Security/wiki/Same_Origin_Policy) is a security policy enforced on\nclient-side web applications (like web browsers) to prevent interactions between\nresources from different origins. While useful for preventing malicious behavior,\nthis security measure also prevents legitimate interactions between known\norigins. For example, a script on a page hosted at\n`example.appspot.com` might need to use resources stored in a\nCloud Storage bucket at `example.storage.googleapis.com`. However, because\nthese are two different origins from the perspective of the browser, the browser\nwon't allow a script from `example.appspot.com` to fetch resources from\n`example.storage.googleapis.com`.\n\nThe [Cross Origin Resource Sharing](https://fetch.spec.whatwg.org/#http-cors-protocol) (CORS) spec was developed by\nthe [World Wide Web Consortium (W3C)](https://www.w3.org/) to get around this limitation.\nCloud Storage supports this specification by allowing you to configure\nyour buckets to support CORS. Continuing the previous example, you can configure\nthe `example.storage.googleapis.com` bucket so that a browser can share its\nresources with scripts from `example.appspot.com`.\n\nFor more information about CORS configuration components, see\n[Set Bucket CORS](/storage/docs/xml-api/put-bucket-metadata#cors-body).\n\nHow CORS works\n--------------\n\nThere are two types of CORS requests: simple and preflighted. A\n[simple request](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests) can be initiated directly. A\n[preflighted request](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests) must send a preliminary, \"preflight\"\nrequest to the server to get permission before the primary request can proceed.\nA request is preflighted if any of the following circumstances are true:\n\n- It uses methods other than `GET`, `HEAD` or `POST`.\n- It uses the `POST` method with a `Content-Type` other than `text/plain`, `application/x-www-form-urlencoded`, or `multipart/form-data`.\n- It sets custom headers. For example, `X-PINGOTHER`.\n\nThe following process occurs when a browser makes a simple request to\nCloud Storage:\n\n1. The browser adds the `Origin` header to the request. The `Origin` header\n contains the origin of the resource seeking to share the\n Cloud Storage bucket's resources, for example,\n `Origin:https://www.example.appspot.com`.\n\n2. Cloud Storage compares the HTTP method of the request and the value\n of the `Origin` header to the **Methods** and **Origins** information in the\n target bucket's CORS configuration to see if there are matches. If there are,\n Cloud Storage includes the `Access-Control-Allow-Origin` header in\n its response. The `Access-Control-Allow-Origin` header contains the value of\n the `Origin` header from the initial request.\n\n3. The browser receives the response and checks to see if the\n `Access-Control-Allow-Origin` value matches the domain specified in the\n original request. If they do match, the request succeeds. If they don't\n match, or if the `Access-Control-Allow-Origin` header is\n not present in the response, the request fails.\n\nA preflighted request performs the following steps first. If it is successful,\nit then follows the same process as a simple request:\n\n1. The browser sends an `OPTIONS` request containing the `Requested Method` and\n `Requested Headers` of the primary request.\n\n2. Cloud Storage responds back with the values of the HTTP methods and headers\n allowed by the targeted resource. If any of the method or header values in\n the preflight request aren't in the set of methods and headers allowed by\n the targeted resource, the request fails, and the primary request isn't\n sent.\n\nThis is a simplified description of CORS. For a more complete description,\nread the [Fetch](https://fetch.spec.whatwg.org/#http-cors-protocol) spec.\n\nCloud Storage CORS support\n--------------------------\n\nCloud Storage lets you set a CORS configuration at the bucket level\nonly. You can [set up a CORS configuration for a bucket](/storage/docs/using-cors) using a variety of\ntools; however, different Cloud Storage [endpoints](/storage/docs/request-endpoints) deal with CORS\nrequests in different ways:\n\n- JSON API endpoints always allow CORS requests and return default values in the\n CORS response headers, regardless of the configuration set on the bucket.\n\n- XML API endpoints only allow CORS requests based on the configuration on the\n bucket and return specific CORS header values in response to that configuration.\n\n- The authenticated browser download endpoint `storage.cloud.google.com` does\n not allow CORS requests. Note that the Google Cloud console provides this\n endpoint for each object's public URL link.\n\nYou can use either of the following XML API request URLs to obtain a response\nfrom Cloud Storage that contains the CORS headers: \n\n```\nstorage.googleapis.com/BUCKET_NAME\n``` \n\n```\nBUCKET_NAME.storage.googleapis.com\n```\n\nFor information about XML API request URLs, see [Request Endpoints](/storage/docs/request-endpoints).\n\nComponents of a CORS configuration\n----------------------------------\n\nWhen using the XML API, the values you set on your bucket's CORS configuration\ndetermine the CORS headers Cloud Storage returns in an HTTP response.\nWhen using the JSON API, Cloud Storage does not evaluate your bucket's\nconfiguration and instead returns default header values.\n\nThe following table describes the fields in a CORS configuration and the\nresponse behavior of the XML and JSON APIs. To learn how these fields are used,\nsee [CORS configuration examples](/storage/docs/cors-configurations).\n\n^1^ Names documented in the Field column are specific to the JSON API.\nWhen using the XML API to set a CORS configuration, use the\n[XML-specific format](/storage/docs/xml-api/put-bucket-metadata#cors-body).\n\n### Specifying multiple origins, methods, or headers\n\nTo learn how to set multiple origins, methods, or headers in a CORS\nconfiguration, see the following list:\n\n- When using the JSON API, you can specify multiple origins, methods, or headers\n by using a comma-separated array. For example, `\"method\": [\"GET\", \"PUT\"]`.\n\n | **Caution:** When you set a CORS configuration using the JSON API, requests from all origins are accepted regardless of the `origin` field value in the CORS configuration.\n- When using the XML API, you can specify multiple origins, methods, or headers\n by using separate elements. For example:\n\n ```xml\n \u003cMethods\u003e\n \u003cMethod\u003ePUT\u003c/Method\u003e\n \u003cMethod\u003eGET\u003c/Method\u003e\n \u003c/Methods\u003e\n ```\n- To allow requests to be made from any origin, set the origin to `*`.\n For example, `\"origin\": [\"*\"]` in the JSON API or `\u003cOrigin\u003e*\u003c/Origin\u003e` in the\n XML API. While this origin is helpful for testing configurations, in most\n cases, you'll want to restrict request origins to prevent unwanted usage of\n your resources.\n\nAdditional considerations\n-------------------------\n\nThe following table describes considerations when making requests using\ncredentials or access control headers:\n\nAllowing buckets to access external resources\n---------------------------------------------\n\nSometimes, you might want to allow scripts hosted in Cloud Storage to\naccess static resources that are hosted on a website external to\nCloud Storage. In this scenario, the website serves CORS headers so\nthat content on `storage.googleapis.com` is allowed access.\n\nAs a best practice, you should dedicate a specific bucket for this data access.\nThis approach prevents your site from inadvertently over-exposing static\nresources to all of `storage.googleapis.com`. For example, if you want to\ndedicate a bucket named `mybucket` for data access, you should have the website\nserve the CORS header\n`Access-Control-Allow-Origin: https://mybucket.storage.googleapis.com` instead\nof `Access-Control-Allow-Origin: https://storage.googleapis.com`.\n\nClient-side CORS support\n------------------------\n\nMost browsers use the `XMLHttpRequest` object to make a cross-domain request.\n`XMLHttpRequest` takes care of all the work of inserting the right headers and\nhandling the CORS interaction with the server. You don't have to add any new\ncode to take advantage of CORS support on Cloud Storage buckets.\n\nWhat's next\n-----------\n\n- Learn how to [enable CORS for your bucket](/storage/docs/using-cors).\n- Explore [CORS configuration examples](/storage/docs/cors-configurations), including an example that removes the CORS configuration on a bucket."]]