Azure blob存储:无法上载大小为0的Blob

时间:2016-07-22 12:27:59

标签: azure azure-storage azure-storage-blobs blobstorage

我们的应用程序需要上传大小为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

你能帮忙弄清楚为什么内容长度标题值被设置为-1?

谢谢和问候, Rahul Naik

2 个答案:

答案 0 :(得分: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”。也许这就是问题所在。

答案 1 :(得分: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}"