CORS Settings
Cross-Origin Resource Sharing (CORS) lets your application load files, images, or other resources directly from an Arvancloud bucket in the browser, even when the application and the bucket are on different domains.
For example, if your application is hosted on https://myapp.example.com and your files are in https://my-bucket.s3.ir-thr-at1.arvanstorage.ir, the browser blocks that request by default. By setting CORS on the bucket, you tell the browser that this cross-origin request is allowed.
Note that CORS only applies to browser requests; API calls from server-side code aren't subject to CORS.
How CORS Works
Browsers enforce a security rule called the same-origin policy, which prevents scripts on one domain from accessing resources on another domain. CORS is a standard mechanism that lets the server (your bucket) declare which other domains it trusts.
For simple requests such as GET, the browser sends the Origin header, and Object Storage returns the response with the Access-Control-Allow-Origin header. For more complex requests, such as PUT or DELETE, or sending custom headers, the browser first sends a preliminary OPTIONS request (preflight) to get permission; if the preflight fails, the main request is blocked.
A request is preflighted when one of the following conditions holds:
- It uses a method other than GET, HEAD, or POST.
- It uses POST with a Content-Type other than
text/plain,application/x-www-form-urlencoded, ormultipart/form-data. - It includes custom headers such as Authorization or x-amz-acl.
CORS Settings Fields
Each CORS rule on a bucket consists of the following fields:
| Field | Required | Description |
|---|---|---|
| Allowed Origins | Yes | The domains allowed to access this bucket. Use * to allow all domains, or enter specific domains such as https://myapp.example.com. |
| Allowed Methods | Yes | The HTTP methods allowed for cross-origin requests. Available values: GET, PUT, POST, DELETE, HEAD. |
| Allowed Headers | No | The headers the browser is allowed to send. Common values: Content-Type, Authorization, x-amz-acl. Use * to allow all headers. |
| Max Age (seconds) | No | How long the browser caches the preflight response. During this time, no extra preflight request is sent for the same resource. Default: 3600 (one hour). |
Note that using * for Allowed Origins is suitable for public files such as images or fonts. For buckets with authenticated uploads or sensitive data, specify exact domains instead of a wildcard. Also note that CORS settings don't affect the bucket's access permissions; a private bucket still requires valid authentication, even with an open CORS rule.
Setting CORS in Object Storage
Log in to the Arvancloud panel and select Object Storage from the side menu. In Buckets, click the actions option and select CORS Settings.
Click New Rule and then fill in the table's fields.
You can define several rules for one bucket. A request only needs to match one of the rules to be allowed. Finally, click Confirm to apply the settings.
Note that CORS changes apply immediately to new requests. Browsers that have already cached the preflight response use the cached version until the Max Age period ends.
Common Configuration Examples
For loading images, fonts, or static files from the bucket in the frontend:
| Field | Value |
|---|---|
| Allowed Origins | https://myapp.example.com |
| Allowed Methods | GET, HEAD |
| Allowed Headers | Content-Type |
| Max Age | 86400 |
For when users upload files directly to the bucket using presigned URLs:
| Field | Value |
|---|---|
| Allowed Origins | https://myapp.example.com |
| Allowed Methods | GET, PUT, POST, DELETE, HEAD |
| Allowed Headers | * |
| Max Age | 3600 |
For buckets with public, non-sensitive content, such as media files or shared assets:
| Field | Value |
|---|---|
| Allowed Origins | * |
| Allowed Methods | GET, HEAD |
| Allowed Headers | * |
| Max Age | 3600 |
Setting CORS via API
You can also set CORS rules through the S3-compatible API. The following example uses the AWS SDK for Python to apply CORS settings to an Arvancloud bucket.
import boto3
s3_client = boto3.client(
's3',
endpoint_url='https://s3.ir-thr-at1.arvanstorage.ir',
aws_access_key_id='<ACCESS-KEY>',
aws_secret_access_key='<SECRET-KEY>',
)
cors_configuration = {
'CORSRules': [
{
'AllowedOrigins': ['https://myapp.example.com'],
'AllowedMethods': ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'],
'AllowedHeaders': ['*'],
'ExposeHeaders': ['ETag', 'Content-Length'],
'MaxAgeSeconds': 3600
}
]
}
s3_client.put_bucket_cors(
Bucket='my-bucket-name',
CORSConfiguration=cors_configuration
)
print("CORS settings applied successfully.")
To get a bucket's current CORS settings:
response = s3_client.get_bucket_cors(Bucket='my-bucket-name')
print(response['CORSRules'])
To completely remove the CORS settings from a bucket:
s3_client.delete_bucket_cors(Bucket='my-bucket-name')
print("CORS settings removed.")
Troubleshooting
-
If the browser shows a CORS error even though you've set a rule:
- The origin in the request must exactly match the value you entered in the panel, including the protocol (https://) and subdomain;
https://myapp.example.comandhttp://myapp.example.comare two different origins. - The HTTP method used must be in the Allowed Methods list. If the request includes an Authorization header or another custom header, that header must be listed in Allowed Headers. For a closer look, click the failed request in the browser's Developer Tools (Network tab) and check the response headers.
- The origin in the request must exactly match the value you entered in the panel, including the protocol (https://) and subdomain;
-
If the OPTIONS preflight request fails:
Make sure your CORS rule includes all methods and headers the actual request uses. Also check the Max Age value; if it's set to 0, the browser sends a new preflight before every request.
-
If CORS works in development but not in production:
The production domain is probably different from the domain you tested during development. Make sure the exact production domain is in the Allowed Origins list, or define a separate CORS rule for it.
-
If you use a custom domain for the bucket:
CORS settings still work. CORS settings apply at the bucket level and work regardless of whether you access the bucket through the default Arvancloud domain or a custom domain.