0

我们的应用程序需要上传大小为 0 的 blob。我们使用 cURL 来调用 Azure 存储 REST API。上传大小时失败并出现 HTTP 错误代码[400]

以下错误消息返回


<?xml version="1.0" encoding="utf-8"?>
    <Error>  
           <Code>InvalidHeaderValue</Code>
           <Message>
                  The value for one of the HTTP headers 
                  is not in the correct format.
                  RequestId:2b1ec18b-0001-007d-7811-e40725000000
                  Time:2016-07-22T12:07:28.5435467Z
          </Message>
          <HeaderName>Content-Length</HeaderName>
          <HeaderValue>-1</HeaderValue>
          </Error>

通过wireshark,我们确保了内容长度头的值被正确发送。

以下是从wireshark捕获的标头


PUT /test/DC70439C-5004-11E6-B4B2-91D87435845D HTTP/1.1
Host: mytest.blob.core.windows.net
Accept: */*
Transfer-Encoding: chunked
x-ms-blob-type:BlockBlob
x-ms-version:2015-02-21
Content-Length:0
x-ms-date:Fri, 22 Jul 2016 12:07:28 GMT
Authorization:SharedKey   kanchan:HQQ7a47TPQtEhL0ek6rim64ZKC8NRubgKuq+4Os+Aoo=
Expect: 100-continue

你能帮忙弄清楚为什么 content-length 标头值被设置为 -1 吗?

谢谢和问候, 拉胡尔·奈克

4

2 回答 2

0

似乎“kanchan”与“mytest”不匹配。我们可以在此链接中找到此信息:https ://msdn.microsoft.com/en-us/library/azure/dd179428.aspx 。

Authorization:SharedKey   kanchan:HQQ7a47TPQtEhL0ek6rim64ZKC8NRubgKuq+4Os+Aoo=

从文档中我们可以发现“kanchan”是账户名。但是,主机信息“mytest.blob.core.windows.net”显示“mytest”。也许这就是问题所在。

于 2016-07-23T03:43:23.637 回答
0

我没有重现你的问题。以下是我使用 cURL 将文件上传到 Azure blob 存储的测试代码。它适用于我,我可以上传大小为 0 的文件(将文件内容设置为“”)。希望这能给你一些提示。

storage_account="<account name>"
container_name="<container name>"
access_key="<account key>"
content="<file content>"
len=${#content}
blobname="<blob name>"
blob_store_url="blob.core.windows.net"
authorization="SharedKey"
request_method="PUT"
request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
storage_service_version="2011-08-18"
# 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_headers="x-ms-blob-type:BlockBlob\n${x_ms_date_h}\n${x_ms_version_h}"
canonicalized_resource="/${storage_account}/${container_name}/${blobname}"
string_to_sign="${request_method}\n\n\n${len}\n\napplication/x-www-form-urlencoded\n\n\n\n\n\n\n${canonicalized_headers}\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)"
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 \
  -X PUT \
  -H "$x_ms_date_h" \
  -H "$x_ms_version_h" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "$authorization_header" \
  -H "x-ms-blob-type:BlockBlob" \
  -d ${content} "https://${storage_account}.${blob_store_url}/${container_name}/${blobname}"
于 2016-07-25T06:56:10.273 回答