簡體   English   中英

帶有Curl的Azure存儲表API REST

[英]Azure Storage Table API REST with Curl

我需要幫助,以在Shell腳本中使用Rest打印實體。 我嘗試了幾種方法,但沒有成功。

這是我的代碼,運行它,它將返回表中的所有實體。

但是我需要它僅返回一個特定值,並且僅使用PartitionKey。

#!/bin/bash

# List the tables in an Azure storage container.

storage_account="Secret"
table_name="teste"
access_key="Secret"

table_store_url="table.core.windows.net"
authorization="SharedKey"

request_method="GET"
request_date=$(TZ=GMT LC_ALL=en_US.utf8 date "+%a, %d %h %Y %H:%M:%S %Z")
#request_date="Mon, 18 Apr 2016 05:16:09 GMT"
storage_service_version="2018-03-28"

# HTTP Request headers
x_ms_date_h="x-ms-date:$request_date"
x_ms_version_h="x-ms-version:$storage_service_version"

# Build the signature string
canonicalized_resource="/${storage_account}/${table_name}"


string_to_sign="${request_method}\n\n\n$request_date\n${canonicalized_resource}"

# Decode the Base64 encoded access key, convert to Hex.
decoded_hex_key="$(echo -n $access_key | base64 -d -w0 | xxd -p -c256)"


# Create the HMAC signature for the Authorization header
signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$decoded_hex_key" -binary |  base64 -w0)

authorization_header="Authorization: $authorization $storage_account:$signature"

curl -s \
  -H "$authorization_header" \
  -H "$x_ms_date_h" \
  -H "$x_ms_version_h" \
  -H "Content-Length: 0" \
  -H "accept: application/json;odata=nometadata" \
 "https://${storage_account}.${table_store_url}/${table_name}"

在這種情況下,我需要更改字符串“ string_to_sign”的末尾和Curl的最后一行。

如果我在末尾插入(PartitionKey ='name',RowKey ='Gabriel')“

得到這樣的兩行:

string_to_sign = "$ {request_method} \ n \ n \ n $ request_date \ n $ {canonicalized_resource} (PartitionKey = 'name', RowKey = Gabriel)"

"https://${storage_account}.${table_store_url}/${table_name}(PartitionKey='nome',RowKey='Gabriel')"

它將僅返回具有PartitionKey“名稱”和Rowkey“ Gabriel”的實體。

但是正如我之前所說,我只能使用分區密鑰。 如果僅將兩行的結尾更改為(PartitionKey ='name'),它將返回以下錯誤:

The number of keys specified in the URI does not match number of key properties for the resource

我嘗試使用$過濾器選項,將兩行的結尾更改為()$filter=(PartitionKey%20ge%20'Name') ,但是在通過Printf時會發生錯誤。

通過放置()$filter=(PartitionKey%%20ge%%20'Name') ,兩個%可以通過,但是在請求時發生身份驗證錯誤。

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature

有人能幫我嗎?

我需要使用PartitionKey,因為在這些測試中,我使用的是我創建的表,並且具有用於測試的RowKey的值,但是在需要實現的地方,我無權訪問RowKey,因此我將傳遞PartitionKey的值,是獲取RowKey結果的電話號碼,該ID是一個ID。

使用了以下文檔:

查詢實體時無需更改string_to_sign ,請參閱Authorization Header

查詢字符串應包含問號和comp參數(例如,?comp = metadata)。 查詢字符串中不應包含其他任何參數。

如果要使用PartitionKey獲取整個實體,請將url更改為($需要編碼為%24)

https://${storage_account}.${table_store_url}/${table_name}?%24filter=PartitionKey%20eq%20'YourPartitionKey'

或者,如果您只想檢索RowKey,請在其后附加&%24select=RowKey

同樣, -H "Content-Length: 0"是沒有用的,只需將其刪除即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM