【问题标题】:Authorization for pkrange query on CosmosDB REST APICosmos DB REST API 上的 pc 范围查询授权
【发布时间】:2017-11-22 15:59:44
【问题描述】:

对于测试场景,我想针对 CosmosDB 数据库中的每个分区运行存储过程,并且我正在尝试使用 PowerShell 从 CosmosDB 获取分区范围列表。

我收到 401 - 此查询的未经授权响应,但同一集合上的其他查询工作正常 - 例如执行过程有效。

我用来查询范围的代码是:

Add-Type -AssemblyName System.Web 

# Configure as required
$accountName  = ""
$connectionKey = ""
$collectionName = ""
$databaseName = ""

function GetKey([System.String]$Verb = '',[System.String]$ResourceId = '',
                [System.String]$ResourceType = '',[System.String]$Date = '',
                [System.String]$masterKey = '')
{
    $keyBytes = [System.Convert]::FromBase64String($masterKey) 
    $text = @($Verb.ToLowerInvariant() + "`n" + 
        $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + 
        $Date.ToLowerInvariant() + "`n" + "" + "`n")
    $body =[Text.Encoding]::UTF8.GetBytes($text)
    $hmacsha = new-object -TypeName System.Security.Cryptography.HMACSHA256 -ArgumentList (,$keyBytes) 
    $hash = $hmacsha.ComputeHash($body)
    $signature = [System.Convert]::ToBase64String($hash)

    [System.Web.HttpUtility]::UrlEncode($('type=master&ver=1.0&sig=' + $signature))
}

function GetUTDate() {
    $date = get-date
    $date = $date.ToUniversalTime();
    return $date.ToString("r", [System.Globalization.CultureInfo]::InvariantCulture);
}       

function BuildHeaders([string]$action = "get",[string]$resType, [string]$resourceId){
    $authz = GetKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $connectionKey
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", $authz)
    $headers.Add("x-ms-version", '2017-02-22')
    $headers.Add("x-ms-date", $apiDate) 
    $headers.Add("Cache-Control", 'no-cache') 
    $headers.Add("Accept", 'application/json')
    $headers.Add("Content-Type", 'application/json')
    $headers
}

function GetPartitionKeys(){
    $pkranges = "dbs/" + $databaseName + "/colls/" + $collectionName + "/pkranges"
    $headers = BuildHeaders -action Get -resType colls -resourceId $pkranges

    $uri = $rootUri + "/" + $pkranges

    write-host "Calling" $uri
    write-host($headers|Out-String)

    $response = Invoke-RestMethod $uri -Method Get -Headers $headers
}

$rootUri = "https://" + $accountName + ".documents.azure.com"
GetPartitionKeys

我认为问题与构建 Auth 标头时的资源类型有关,但 CosmosDB REST 文档没有太多关于查询此资源的信息。目前我得到以下输出:

Calling https://my-account-name.documents.azure.com/dbs/my-db-name/colls/my-coll-name/pkranges

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

【问题讨论】:

    标签: powershell azure azure-cosmosdb


    【解决方案1】:

    在计算授权标头的 has 时,ResourceType 值应为“pkranges”。

    ResourceLink 将是“dbs/my-db-name/colls/my-coll-name”,您请求的 URL 是:https://my-account-name.documents.azure.com/dbs/my-db-name/colls/my-coll-name/pkranges

    【讨论】:

      【解决方案2】:

      Paul- 存储过程只能针对分区键执行,不能针对单个分区键范围。

      【讨论】:

      • 嗨,Mimi,是的,如果我指定了一个我知道存在的分区键,我就可以运行我的存储过程。但是,我需要以编程方式为每个分区找到一个密钥,然后为每个分区运行该过程。我的程序是在自动测试运行之前从集合中删除所有数据(可能低于 100 条记录)。删除和重新创建集合是相当喜怒无常的,而且经常出错,所以我正在尝试这条路线。
      猜你喜欢
      • 2021-04-13
      • 2019-06-28
      • 2021-11-05
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2020-09-20
      • 1970-01-01
      相关资源
      最近更新 更多