简体   繁体   English

带有Curl的Azure存储表API REST

[英]Azure Storage Table API REST with Curl

I need help to print an entity with Rest in shell script. 我需要帮助,以在Shell脚本中使用Rest打印实体。 I tried several ways, but without success. 我尝试了几种方法,但没有成功。

Here is my code, running it, it will return all entities in my table. 这是我的代码,运行它,它将返回表中的所有实体。

But I need it to return only a specific value and that is only using PartitionKey. 但是我需要它仅返回一个特定值,并且仅使用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}"

In this case, I need to change the end of the string "string_to_sign" and the last line of the Curl. 在这种情况下,我需要更改字符串“ string_to_sign”的末尾和Curl的最后一行。

If I insert at the end (PartitionKey='name',RowKey = 'Gabriel')" 如果我在末尾插入(PartitionKey ='name',RowKey ='Gabriel')“

Getting the two lines like this: 得到这样的两行:

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')"

It will return only the entity with PartitionKey "name" and Rowkey "Gabriel". 它将仅返回具有PartitionKey“名称”和Rowkey“ Gabriel”的实体。

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: 如果仅将两行的结尾更改为(PartitionKey ='name'),它将返回以下错误:

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

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') ,但是在通过Printf时会发生错误。

By putting ()$filter=(PartitionKey%%20ge%%20'Name') , Two %, it passes, but occurs authentication error on request. 通过放置()$filter=(PartitionKey%%20ge%%20'Name') ,两个%可以通过,但是在请求时发生身份验证错误。

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

Can someone help me? 有人能帮我吗?

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. 我需要使用PartitionKey,因为在这些测试中,我使用的是我创建的表,并且具有用于测试的RowKey的值,但是在需要实现的地方,我无权访问RowKey,因此我将传递PartitionKey的值,是获取RowKey结果的电话号码,该ID是一个ID。

Used the following documentation: 使用了以下文档:

No need to change string_to_sign when querying entity, see Authorization Header . 查询实体时无需更改string_to_sign ,请参阅Authorization Header

The query string should include the question mark and the comp parameter (for example, ?comp=metadata). 查询字符串应包含问号和comp参数(例如,?comp = metadata)。 No other parameters should be included on the query string. 查询字符串中不应包含其他任何参数。

If you want to get whole entity using PartitionKey, change url to($ need encoding to %24) 如果要使用PartitionKey获取整个实体,请将url更改为($需要编码为%24)

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

Or if you only want to retrieve RowKey, append &%24select=RowKey after that. 或者,如果您只想检索RowKey,请在其后附加&%24select=RowKey

Also -H "Content-Length: 0" is useless, just delete it. 同样, -H "Content-Length: 0"是没有用的,只需将其删除即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM