存储服务API调用

时间:2017-01-25 14:05:54

标签: bash azure curl azure-storage-files

我尝试通过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

documentation说,&#34;授权&#34;是必需的(语法:Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>")和"Signature" is a Hash-based Message Authentication Code (HMAC) constructed from the request and computed by using the SHA256 algorithm, and then encoded by using Base64 encoding. 那么如何生成此签名?

1 个答案:

答案 0 :(得分:2)

尝试使用bash脚本创建分享

#!/bin/sh

STORAGE_KEY="$1"
STORAE_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="/$STORAE_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 $STORAE_ACCOUNT:$SIGN" \
  -H "Content-Length:0" \
  "https://$STORAE_ACCOUNT.file.core.windows.net/$SHARE_NAME?restype=share"

尝试在指定的共享下创建目录

#!/bin/sh

STORAGE_KEY="$1"
STORAE_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="/$STORAE_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 $STORAE_ACCOUNT:$SIGN" \
  -H "Content-Length:0" \
  "https://$STORAE_ACCOUNT.file.core.windows.net/$SHARE_NAME/$DIRECTORY_NAME?restype=directory"