1

我尝试通过 bash 脚本在现有 Azure 存储帐户上创建文件共享。我只有帐户名和密钥,但不想使用登录凭据。这是我到目前为止所拥有的:

#!/bin/sh

DATE_ISO=$(date +"%Y-%m-%dT%H:%M:%S")
VERSION="2015-02-21"  

curl --header "x-ms-version: ${VERSION}" --header "x-ms-date: ${DATE_ISO}" --header "Authorization: SharedKey mystorageaccount:?????" https://mystorageaccount.file.core.windows.net/myshare?restype=share

文档说, “授权”是必需的(语法:)Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>"“签名”是从请求构造并使用 SHA256 算法计算的基于哈希的消息身份验证代码 (HMAC),然后使用 Base64 编码进行编码。 那么如何生成这个签名呢?

4

1 回答 1

4

试试这个用脚本创建共享。bash

#!/bin/sh

STORAGE_KEY="$1"
STORAGE_ACCOUNT="$2"
SHARE_NAME="$3"

DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
VERSION="2015-12-11"
HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
URL_RESOURCE="/$STORAGE_ACCOUNT/$SHARE_NAME\nrestype:share"
STRING_TO_SIGN="PUT\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"

DECODED_KEY="$(echo -n $STORAGE_KEY | base64 -d -w0 | xxd -p -c256)"
SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary |  base64 -w0)

curl -X PUT \
  -H "x-ms-date:$DATE_ISO" \
  -H "x-ms-version:$VERSION" \
  -H "Authorization: SharedKey $STORAGE_ACCOUNT:$SIGN" \
  -H "Content-Length:0" \
  "https://$STORAGE_ACCOUNT.file.core.windows.net/$SHARE_NAME?restype=share"

试试这个在指定的共享下创建目录。

#!/bin/sh

STORAGE_KEY="$1"
STORAGE_ACCOUNT="$2"
SHARE_NAME="$3"
DIRECTORY_NAME="$4"

DATE_ISO=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
VERSION="2015-12-11"
HEADER_RESOURCE="x-ms-date:$DATE_ISO\nx-ms-version:$VERSION"
URL_RESOURCE="/$STORAGE_ACCOUNT/$SHARE_NAME/$DIRECTORY_NAME\nrestype:directory"
STRING_TO_SIGN="PUT\n\n\n\n\n\n\n\n\n\n\n\n$HEADER_RESOURCE\n$URL_RESOURCE"

DECODED_KEY="$(echo -n $STORAGE_KEY | base64 -d -w0 | xxd -p -c256)"
SIGN=$(printf "$STRING_TO_SIGN" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$DECODED_KEY" -binary |  base64 -w0)

curl -X PUT \
  -H "x-ms-date:$DATE_ISO" \
  -H "x-ms-version:$VERSION" \
  -H "Authorization: SharedKey $STORAGE_ACCOUNT:$SIGN" \
  -H "Content-Length:0" \
  "https://$STORAGE_ACCOUNT.file.core.windows.net/$SHARE_NAME/$DIRECTORY_NAME?restype=directory"
于 2017-01-26T09:11:50.747 回答