- Print
- DarkLight
Enable CORS with the CLI
- Print
- DarkLight
You can use the Backblaze B2 Cloud Storage command-line interface (CLI) to implement cross-origin resource sharing (CORS) rules.
Access Arguments
Use the following command to access the arguments to set the CORS rules:
b2 update-bucket [--bucketInfo <json>] [--corsRules <json>] [--lifecycleRules <json>] <bucketName> [allPublic | allPrivate]
Add CORS Rules
The easiest way to add the rules is with a JSON file that contains the rule set. You can copy the following example and save the JSON block locally.
[
{
"corsRuleName": "downloadFromAnyOrigin",
"allowedOrigins": [
"https"
],
"allowedHeaders": [
"range"
],
"allowedOperations": [
"b2_download_file_by_id",
"b2_download_file_by_name"
],
"exposeHeaders": [
"x-bz-content-sha1"
],
"maxAgeSeconds": 3600
}
]
After you validate the JSON file with your rule set, add the rule set to your bucket.
In the following example, the JSON file is named rules.json
and it is located in the current working directory.
Run the following command to add the rule set to your bucket. You can use this example after you have a validated JSON file of rules, but the command will make the specified bucket "public."
b2 update-bucket --corsRules "$(<./rules.json)" bucketName allPublic
The following example shows a possible output:
{
"accountId": "d9xxxf5427b",
"bucketId": "dxxxxx2dec998xxxxx02071b",
"bucketInfo": {},
"bucketName": "TestBucket",
"bucketType": "allPublic",
"corsRules": [
{
"allowedHeaders": [
"range"
],
"allowedOperations": [
"b2_download_file_by_id",
"b2_download_file_by_name"
],
"allowedOrigins": [
"https"
],
"corsRuleName": "downloadFromAnyOrigin",
"exposeHeaders": [
"x-bz-content-sha1"
],
"maxAgeSeconds": 3600
}
],
"lifecycleRules": [],
"revision": 4
}
Failures
The failures that can occur are typically based around an invalid JSON file. Backblaze recommends that you use a validation tool like jq on the file if you encounter issues.
Use CORS through the CLI without a File
To use the CORS rules through the CLI without a file, escape all of the double quotes around the key pairs or use single quotes around the JSON block, as in the following example:
b2 update-bucket --corsRules '[{"corsRuleName":"downloadFromAnyOrigin", "allowedOrigins": ["https"], "allowedHeaders": ["range"], "allowedOperations": ["b2_download_file_by_id", "b2_download_file_by_name"], "exposeHeaders": ["x-bz-content-sha1"], "maxAgeSeconds": 3600}]' bucketName allPublic
The command applies the same values and the method of calling the JSON through a file.
Use double-quotes (") to surround the JSON key pairs, this set of double-quotes is not escaped. These are the first and last double quotes in the code block above.
Windows B2 Self-Contained CLI
The Windows CLI does not respond appropriately with the above command-line string. This is due to the way Windows handles the single quote.
To correct this issue, replace the leading and tailing single-quote with a double quote and escape any double-quotes inside of the JSON:
b2-windows.exe update-bucket --corsRules "[{\"corsRuleName\":\"downloadFromAnyOrigin\", \"allowedOrigins\": [\"https\"], \"allowedHeaders\": [\"range\"], \"allowedOperations\": [\"b2_download_file_by_id\", \"b2_download_file_by_name\"], \"exposeHeaders\": [\"x-bz-content-sha1\"], \"maxAgeSeconds\": 3600}]" bucketName allPublic