Storage service API call

一曲冷凌霜 提交于 2019-12-24 07:17:48

问题


I try to create a file share on an existing Azure storage account via bash script. I only have the account name and key, but don't want to use login credentials. This is what I have so far:

#!/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

The documentation says, "Authorization" is required (syntax: Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>") and "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. So how do I generate this Signature?


回答1:


Try this to create Share with bash script.

#!/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"

Try this to create Directory under the specified 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"


来源:https://stackoverflow.com/questions/41853666/storage-service-api-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!