- 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 bucket update [--bucket-info <json>] [--cors-rules <json>] [--lifecycle-rules <json>] <bucketName> [allPublic | allPrivate]Add CORS Rules with a JSON File (MacOS and Linux)
The easiest way to add the rules is with a JSON file that contains the rule set.
- 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.jsonand 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:"
The following example shows a possible output:b2 bucket update --cors-rules "$(<./rules.json)" <bucketName> <allPublic/allPrivate>{ "accountId": "xxxxxxxxxxx", "bucketId": "xxxxxxxxxxxxxxxxxxxxxxx", "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": #
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.
Add CORS Rules without a File (MacOS and Linux)
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 bucket update --cors-rules '[{\"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/allPrivate>The command applies the same values as 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.
Add CORS Rules with a JSON File (Windows PowerShell)
Before you begin:
The steps for enabling CORS differ depending on your PowerShell version. To check your version, run the following command:
$PSVersionTable- PowerShell 5.x shows
PSEdition: Desktop - PowerShell 6 or later shows
PSEdition: Core
Follow the instructions below that match your PowerShell version.
Add CORS Rules using Powershell 5.x (Desktop)
PowerShell 5.x does not support the same JSON handling methods as PowerShell 6+. When using PowerShell 5.x, you must convert the JSON file into a string before passing it to the Backblaze CLI.
- Create a rules.json file containing your CORS rules and save it locally.
- Convert the JSON file into a string:
$corsRules = (Get-Content "\path\to\rules.json" -Raw) -replace '\s', '' -replace '"', '\"'-replace '\s', ''removes all whitespace characters (such as newlines and carriage returns).-replace '"', '\"'escapes double quotes so PowerShell can pass the string correctly to the CLI.- PowerShell 5.x requires CORS rules to be passed as a string rather than as a JSON object.
- Verify the output:
The following example shows a possible output:echo $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}] - Update the bucket with the CORS rules:
b2 bucket update --cors-rules $corsRules <bucketName> <allPublic|allPrivate>
Add CORS Rules using PowerShell 6 and later (Core)
PowerShell 6+ supports passing JSON objects directly to the Backblaze CLI.
- Create a rules.json file.
- Verify that PowerShell correctly interprets the file using the following command:
Get-Content -Raw "path\to\rules.json" | ConvertFrom-Json - Create a PowerShell Object using the file rules.json:
$corsRules = Get-Content "path\to\rules.json" -Raw - Use the Object to set CORS rules:
B2 bucket update --cors-rules $corsRules <bucketName> <allPublic/allPrivate>
Add CORS Rules without a File (Windows PowerShell)
Before you begin:
The steps for enabling CORS differ depending on your PowerShell version. To check your version, run the following command:
$PSVersionTable- PowerShell 5.x shows
PSEdition: Desktop - PowerShell 6 or later shows
PSEdition: Core
Follow the instructions below that match your PowerShell version.
Add CORS Rules without a File Windows PowerShell 5.x (Desktop)
PowerShell Version 5.x and earlier accepts a single quote at the opening and closing of the JSON block. However, you must escape double-quotes within the JSON block.
b2 bucket update --cors-rules '[{\"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/allPrivate>Add CORS Rules without a File Windows PowerShell 6 and later (Core)
With PowerShell 6 and later, you must use a backtick “ ` ” within the JSON block to escape double-quotes rather than a backslash. You can use double-quotes to open and close the JSON block.
b2 bucket update --cors-rules "[{`"corsRuleName`":`"downloadFromAnyOrigin`",`"allowedOrigins`":[`"https`"],`"allowedHeaders`":[`"range`"],`"allowedOperations`":[`"b2_download_file_by_id`",`"b2_download_file_by_name`"],`"exposeHeaders`":[`"x-bz-content-sha1`"],`"maxAgeSeconds`":3600}]"<bucket> <allPublic/allPrivate>Add CORS Rules with a File (Windows Command Prompt)
When using a variable in Command Prompt, the entire command line string can be only 8,191 characters. This includes CLI commands along with all flags and options.
- Create a rules.json file with your JSON block on a single line:
[{\"corsRuleName\":\"downloadFromAnyOrigin\",\"allowedOrigins\":[\"https\"],\"allowedHeaders\":[\"range\"],\"allowedOperations\":[\"b2_download_file_by_id\",\"b2_download_file_by_name\"],\"exposeHeaders\":[\"x-bz-content-sha1\"],\"maxAgeSeconds\":3600}] - Create a variable using the command
set:set /p corsRules=</path/to/rules.json - Verify that the content of the variable is correct:
The output should be similar to the following example:echo %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}] - Use the following variable to set CORS rules:
B2 bucket update --cors-rules %corsRules% <bucketName> <allPublic/allPrivate>
Add CORS Rules without a File (Windows Command Prompt)
The Windows CLI does not respond appropriately with the above command-line string. This is due to the way Windows handles the single quote. Replace the leading and tailing single-quote with a double quote and escape any double-quotes with a backslash “ \ ” inside of the JSON block:
b2 bucket update --cors-rules "[{\"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/allPrivate>