我已查看了以下信息:
- https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob
- https://docs.microsoft.com/en-us/rest/api/storageservices/service-sas-examples
据我所知,我正在遵守,因为我在 URL 中发布了 SAS。
这是我的简单测试脚本:
#!/bin/bash
DATE_NOW=$(date -Ru | sed 's/\+0000/GMT/')
AZ_VERSION="2018-03-28"
AZ_BLOB_URL="https://XXXXXXXX.blob.core.windows.net"
AZ_BLOB_CONTAINER="XXXXXX"
AZ_BLOB_TARGET="${AZ_BLOB_URL}/${AZ_BLOB_CONTAINER}/"
AZ_SAS_TOKEN="?sv=2017-11-09&ss=b&srt=o&sp=rwlac&se=2021-11-06T01:05:33Z&st=2018-11-09T17:05:33Z&sip=192.0.2.0-192.0.2.254&spr=https&sig=XXXXXXXXXX"
curl -v -X PUT -H "Content-Type: application/octet-stream" -H "x-ms-date: ${DATE_NOW}" -H "x-ms-version: ${AZ_VERSION}" --data-binary "@/home/test/test" "${AZ_BLOB_TARGET}test${AZ_SAS_TOKEN}"
运行结果为:
> PUT /XXXX/XXXXX?sv=2017-11-09&ss=b&srt=o&sp=rwlac&se=2021-11-06T01:05:33Z&st=2018-11-09T17:05:33Z&sip=192.0.2.0-192.0.2.254&spr=https&sig=XXXXXXXXXX" HTTP/1.1
> Host: XXXXX.blob.core.windows.net
> User-Agent: curl/7.60.0
> Accept: */*
> Content-Type: application/octet-stream
> x-ms-date: Sat, 10 Nov 2018 17:48:06 GMT
> x-ms-version: 2018-03-28
> Content-Length: 14
>
* upload completely sent off: 14 out of 14 bytes
< HTTP/1.1 400 An HTTP header that's mandatory for this request is not specified.
< Content-Length: 295
< Content-Type: application/xml
< Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
< x-ms-request-id: 69b144ec-b01e-0112-651d-791ccd000000
< x-ms-version: 2018-03-28
< x-ms-error-code: MissingRequiredHeader
< Date: Sat, 10 Nov 2018 17:48:06 GMT
<
<?xml version="1.0" encoding="utf-8"?><Error><Code>MissingRequiredHeader</Code><Message>An HTTP header that's mandatory for this request is not specified.
RequestId:69b144ec-b01e-0112-651d-791ccd000000
答案1
对你的问题的简单回答是,你缺少了-H "x-ms-blob-type: BlockBlob"
。
但是,修复标头后,您的 SAS 令牌也可能无效,您尝试使用没有容器访问权限且可能有 IP 限制的 SAS 令牌上传文件。
#!/bin/bash
DATE_NOW=$(date -Ru | sed 's/\+0000/GMT/')
AZ_VERSION="2018-03-28"
AZ_BLOB_URL="https://xxxxxx.blob.core.windows.net"
AZ_BLOB_CONTAINER="test"
AZ_BLOB_TARGET="${AZ_BLOB_URL}/${AZ_BLOB_CONTAINER}/"
AZ_SAS_TOKEN="?sv=2017-11-09&ss=b&srt=sco&sp=rwlac&se=2018-11-11T05:03:03Z&st=2018-11-10T21:03:03Z&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D"
curl -v -X PUT -H "Content-Type: application/octet-stream" -H "x-ms-date: ${DATE_NOW}" -H "x-ms-version: ${AZ_VERSION}" -H "x-ms-blob-type: BlockBlob" --data-binary "/temp/test.log" "${AZ_BLOB_TARGET}test.log${AZ_SAS_TOKEN}"
希望这可以帮助。