带有Curl的Azure存储表API REST [英] Azure Storage Table API REST with Curl
问题描述
我需要帮助以Shell脚本打印带有Rest的实体.我尝试了几种方法,但没有成功.
I need help to print an entity with Rest in shell script. I tried several ways, but without success.
这是我的代码,正在运行它,它将返回表中的所有实体.
Here is my code, running it, it will return all entities in my table.
但是我需要它仅返回一个特定值,并且仅使用PartitionKey.
But I need it to return only a specific value and that is only using 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的最后一行.
In this case, I need to change the end of the string "string_to_sign" and the last line of the Curl.
如果我在末尾插入(PartitionKey ='name',RowKey ='Gabriel')"
If I insert at the end (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"的实体.
It will return only the entity with PartitionKey "name" and Rowkey "Gabriel".
但是正如我之前所说,我只能使用分区密钥.如果仅将两行的结尾更改为(PartitionKey ='name'),它将返回以下错误:
But as I said before, I must use only Partition Key. If I change the end of the two lines to (PartitionKey = 'name') only, it returns the following error:
The number of keys specified in the URI does not match number of key properties for the resource
我尝试使用$过滤器选项,将两行的结尾更改为()$ filter =(PartitionKey%20ge%20'Name')
,但是在通过打印文件.
I tried using the $ filter option, changing the end of the two lines to ()$filter=(PartitionKey%20ge%20'Name')
, however it happens error when passing through the Printf.
通过放置()$ filter =(PartitionKey %% 20ge %% 20'Name')
,两个%可以通过,但是在请求时出现身份验证错误.
By putting ()$filter=(PartitionKey%%20ge%%20'Name')
, Two %, it passes, but occurs authentication error on request.
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature
有人可以帮助我吗?
我需要使用PartitionKey,因为在这些测试中,我使用的是我创建的表,并且具有用于测试的RowKey的值,但是在需要实现的地方,我无权访问RowKey,因此我将传递该值PartitionKey是电话号码,用于获取RowKey的结果,该RowId是一个ID.
I need to use PartitionKey because in these tests I am using a table that I created and I have the value of RowKey for tests, but where I need to implement, I do not have access to RowKey, I would pass the value of PartitionKey which is a telephone number to obtain the result of the RowKey, which is an Id.
使用了以下文档:
- https://docs.microsoft.com/en-us/rest/api/storageservices/table-service-rest-api
- https://docs.microsoft.com/en-us/rest/api/storageservices/query-entities
推荐答案
No need to change string_to_sign
when querying entity, see Authorization Header.
查询字符串应包含问号和comp参数(例如,?comp = metadata).查询字符串中不应包含其他任何参数.
The query string should include the question mark and the comp parameter (for example, ?comp=metadata). No other parameters should be included on the query string.
如果要使用PartitionKey获取整个实体,请将url更改为($需要编码为%24)
If you want to get whole entity using PartitionKey, change url to($ need encoding to %24)
https://${storage_account}.${table_store_url}/${table_name}?%24filter=PartitionKey%20eq%20'YourPartitionKey'
或者,如果您只想检索RowKey,请在其后附加&%24select = RowKey
.
Or if you only want to retrieve RowKey, append &%24select=RowKey
after that.
也 -H"Content-Length:0"
是没有用的,只需将其删除即可.
Also -H "Content-Length: 0"
is useless, just delete it.
这篇关于带有Curl的Azure存储表API REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!