【问题标题】:Azure Storage Table API REST with Curl带有 Curl 的 Azure 存储表 API REST
【发布时间】:2018-12-06 22:04:37
【问题描述】:

我需要帮助以在 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 "name" 和 Rowkey "Gabriel" 的实体。

但正如我之前所说,我必须只使用分区键。如果我将两行的末尾更改为 (PartitionKey = 'name') only,它会返回以下错误:

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'), 2%,它通过了,但在请求时发生身份验证错误。

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。

使用了以下文档:

【问题讨论】:

    标签: bash rest azure curl azure-table-storage


    【解决方案1】:

    查询实体时无需更改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"没用,删掉就好了。

    【讨论】:

    • 非常感谢,杰瑞!看到这段代码,我发现问题出在“$filter”部分,然后改成“%24filter”就可以了。
    猜你喜欢
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多