Home > Blockchain >  How to edit existing Azure storage account CORS Rule through Bash Script
How to edit existing Azure storage account CORS Rule through Bash Script

Time:02-23

I've been working as Devops engineer from past 3 months. So, I am learning creating scripts for automation. But I am stuck here. I wanna try to edit the CORS rule through bash script. I have successfully written a script for adding the rule but every time i run the script, its create a new rule. I want to edit the existing rule.

Here's the script line, I am using to add CORS rule.

Add_Rule=$(az storage cors add --account-name testingscriptcors --origins 'http://google321.comh, http://www.google123.com'  --methods GET PUT --allowed-headers '*' --exposed-headers '*' --max-age 200 --services b)

What I am thinking that if I provide ( --rule 1 ) in this script, It'll worked out. But this doesn't work.

CodePudding user response:

You can add and edit the existing rule using Set-AzStorageCORSRule. You can approch this way as i have mentioned here.

Cmdlet to Set or add the CORS rule

$ctx=New-AzStorageContext -StorageAccountName "clouXXXXXXXman" -StorageAccountKey "ExXXXXXXXXXzUgkXi80HHrXXXXXXXXXXXXXXXXXXXXX"
$CorsRules = (@{
AllowedHeaders=@("x-ms-blob-content-type","x-ms-blob-content-disposition");
AllowedOrigins=@("*");
MaxAgeInSeconds=30;
AllowedMethods=@("Get","Connect")},
@{
AllowedOrigins=@("http://www.fabrikam.com","http://www.contoso.com");
ExposedHeaders=@("x-ms-meta-data*","x-ms-meta-customheader");
AllowedHeaders=@("x-ms-meta-target*","x-ms-meta-customheader");
MaxAgeInSeconds=30;
AllowedMethods=@("Put")})
Set-AzStorageCORSRule -ServiceType Blob -CorsRules $CorsRules -Context $ctx

Once Set you can check the list of rule using below command

az storage cors list --account-name cloudXXXXXXn --account-key ExlYLcXXXXXXXXXXXXXXXXXX

enter image description here

Change properties of a CORS rule for blob service

$ctx1=New-AzStorageContext -StorageAccountName "cloXXXXXuman" -StorageAccountKey "ExlYLc5XXXXXXXXXXXXXXXXXXXXXXXXXXX"
$CorsRules = Get-AzStorageCORSRule -ServiceType Blob -Context $ctx1
$CorsRules[0].AllowedHeaders = @("x-ms-blob-content-type", "x-ms-blob-content-disposition")
$CorsRules[0].AllowedMethods = @("Get", "Connect", "Merge")
Set-AzStorageCORSRule -ServiceType Blob -CorsRules $CorsRules -Context $ctx1

enter image description here

You can refer this MS Document for more information.

  • Related