Overview Configuration samples
Cross Origin Resource Sharing (CORS) allows interactions between resources from different origins, something that is normally prohibited in order to prevent malicious behavior. Use this page to learn how to set a CORS configuration on a Cloud Storage bucket and how to view the CORS configuration set on a bucket. See Configuration examples for CORS for example CORS configurations, including the configuration that disables any existing configuration on your bucket.
Required roles
To get the permissions that you need to set and view the CORS configuration
on a bucket, ask your administrator to grant you the Storage Admin
( roles/storage.admin
) role on the bucket.
This predefined role contains the permissions required to set and view CORS configurations. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
-
storage.buckets.get
-
storage.buckets.update
You can also get these permissions with other predefined roles or custom roles .
For information about granting roles on buckets, see Use IAM with buckets .
Set the CORS configuration on a bucket
You set a CORS configuration on a bucket by specifying information, such as HTTP methods and originating domains, that identify the types of requests the bucket can accept.
Use the following steps to set a CORS configuration on your bucket:
Console
You cannot manage CORS using the Google Cloud console. Use the gcloud CLI instead.
Command line
-
Create a JSON file with the CORS configuration you would like to apply. See configuration examples for sample JSON files.
-
Use the
gcloud storage buckets update
command with the--cors-file
flag:gcloud storage buckets update gs:// BUCKET_NAME --cors-file= CORS_CONFIG_FILE
Where:
-
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
. -
CORS_CONFIG_FILE
is the path to the JSON file you created in Step 1.
-
Client libraries
C++
For more information, see the Cloud Storage C++ API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
C#
For more information, see the Cloud Storage C# API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
Go
For more information, see the Cloud Storage Go API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
Java
For more information, see the Cloud Storage Java API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
Node.js
For more information, see the Cloud Storage Node.js API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
PHP
For more information, see the Cloud Storage PHP API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
Python
For more information, see the Cloud Storage Python API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket:
Ruby
For more information, see the Cloud Storage Ruby API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample sets a CORS configuration on a bucket: