Azure Storage Table API REST with Curl

不羁岁月 提交于 2021-02-07 04:38:35

问题


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.

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}"

In this case, I need to change the end of the string "string_to_sign" and the last line of the Curl.

If I insert at the end (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".

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

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.

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

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.

Used the following documentation:

  • https://docs.microsoft.com/en-us/rest/api/storageservices/table-service-rest-api
  • https://docs.microsoft.com/en-us/rest/api/storageservices/query-entities

回答1:


No need to change string_to_sign when querying entity, see Authorization Header.

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.

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'

Or if you only want to retrieve RowKey, append &%24select=RowKey after that.

Also -H "Content-Length: 0" is useless, just delete it.



来源:https://stackoverflow.com/questions/51072445/azure-storage-table-api-rest-with-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!