Как добавить правило CORS в эмулятор хранилища Azure с HTTP?

Чтобы использовать табличную службу хранилища (эмулятора) Azure, мне нужно добавить правило CORS для моего приложения браузера TypeScript.

Я хочу добавить это правило вручную, используя интерфейс REST (от Почтальона, а не от Браузера с одинаковой политикой происхождения). В документации не указан правильный URL-адрес для эмулятора ( https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-table-service-properties). Для команд DML это как в моем запросе ( https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/insert-entity).

Запрос:

PUT /devstoreaccount1/?restype=service&comp=properties HTTP/1.1
Host: 127.0.0.1:10002
x-ms-version: 2013-08-15
Content-Type: application/xml
Cache-Control: no-cache
Postman-Token: 280f880b-d6df-bb1d-bc12-eca411e18310

<StorageServiceProperties>
    <Cors>
        <CorsRule>
            <AllowedOrigins>http://localhost:3030</AllowedOrigins>
            <AllowedMethods>GET,PUT,POST</AllowedMethods>
            <MaxAgeInSeconds>500</MaxAgeInSeconds>
            <ExposedHeaders>x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-abc</ExposedHeaders>
            <AllowedHeaders>x-ms-meta-*</AllowedHeaders>
        </CorsRule>
    </Cors>
</StorageServiceProperties>

Результат:

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>ResourceNotFound</m:code>
    <m:message xml:lang="en-US">The specified resource does not exist.
RequestId:8137042f-0402-46c6-aa8c-fbf9f4601d33
Time:2017-01-15T09:13:51.7500394Z</m:message>
</m:error>

Какой правильный URL или что я делаю не так?

3 ответа

Если вы загружаете Microsoft Azure Storage Explorer, вы можете настроить его, щелкнув правой кнопкой мыши "Контейнеры BLOB-объектов" под своей учетной записью.

Вот скрипт Powershell для добавления правила CORS в эмулятор хранилища Azure. Это не ответ на этот вопрос, а решение моей проблемы:

$ErrorActionPreference = "Stop";

# config

$AccountName='devstoreaccount1'
$AccountKey='Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='

# derived config

$BlobEndpoint="http://127.0.0.1:10000/$($AccountName)"
$QueueEndpoint="http://127.0.0.1:10001/$($AccountName)"
$TableEndpoint="http://127.0.0.1:10002/$($AccountName)"

$ConnectionString = "" +
    "DefaultEndpointsProtocol=http;" + 
    "BlobEndpoint=$($BlobEndpoint);" +
    "QueueEndpoint=$($QueueEndpoint);" +
    "TableEndpoint=$($TableEndpoint);" +
    "AccountName=$($AccountName);" +
    "AccountKey=$($AccountKey)"

# authentication

$Context = New-AzureStorageContext `
    -ConnectionString $ConnectionString

# cors rules
$CorsRules = (@{
    AllowedHeaders=@("*");
    AllowedOrigins=@("*");
    ExposedHeaders=@("Content-Length");
    MaxAgeInSeconds=60*60*24;
    AllowedMethods=@("Get", "Post")
})

Set-AzureStorageCORSRule `
    -ServiceType Table `
    -Context $Context `
    -CorsRules $CorsRules

# check
Get-AzureStorageCORSRule `
    -ServiceType Table `
    -Context $Context

Я скорректировал сообщение @abbgrade для работы с последней версией Powershell (протестировано с 7.1.3). Спасибо, abbgrade, за полезный вклад. Я пытался заставить его работать с az cli, но в конце концов это сработало!

      Install-Module Az.Storage

$AccountName='devstoreaccount1'
$AccountKey='Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='
$BlobEndpoint="http://127.0.0.1:10000/$($AccountName)"
$QueueEndpoint="http://127.0.0.1:10001/$($AccountName)"
$TableEndpoint="http://127.0.0.1:10002/$($AccountName)"

$ConnectionString = "" +
    "DefaultEndpointsProtocol=http;" + 
    "BlobEndpoint=$($BlobEndpoint);" +
    "QueueEndpoint=$($QueueEndpoint);" +
    "TableEndpoint=$($TableEndpoint);" +
    "AccountName=$($AccountName);" +
    "AccountKey=$($AccountKey)"

$Context = New-AzStorageContext ` -ConnectionString $ConnectionString

$CorsRules = (@{
    AllowedHeaders=@("*");
    AllowedOrigins=@("*");
    ExposedHeaders=@("*");
    MaxAgeInSeconds=60*60*24;
    AllowedMethods=@("Get", "Post", "Put", "Delete", "Head", "Options")
})

Set-AzStorageCORSRule `
    -ServiceType Blob `
    -Context $Context `
    -CorsRules $CorsRules

Set-AzStorageCORSRule `
    -ServiceType Queue `
    -Context $Context `
    -CorsRules $CorsRules

Set-AzStorageCORSRule `
    -ServiceType Table `
    -Context $Context `
    -CorsRules $CorsRules

Write-Host "CORS for Blob:"
Get-AzStorageCORSRule `
    -ServiceType Blob `
    -Context $Context

Write-Host "CORS for Queue:"
Get-AzStorageCORSRule `
    -ServiceType Queue `
    -Context $Context

Write-Host "CORS for Table:"
Get-AzStorageCORSRule `
    -ServiceType Table `
    -Context $Context

Write-Host "CORS rules for Azurite have been configured."
Другие вопросы по тегам