如何使用HTTP将CORS规则添加到Azure存储模拟器?

时间:2017-01-15 09:46:16

标签: azure cors azure-storage azure-table-storage azure-storage-emulator

要使用Azure存储(模拟器)表服务,我需要为我的TypeScript浏览器应用程序添加CORS规则。

我想使用REST接口手动添加该规则(来自Postman,而不是具有相同原始策略的浏览器)。 文档未提供模拟器的正确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>

什么是正确的网址或我做错了什么?

2 个答案:

答案 0 :(得分:8)

以下是一个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

答案 1 :(得分:1)

如果下载Microsoft Azure Storage Explorer,则可以通过右键单击帐户下的“ Blob容器”来进行设置。

enter image description here