【问题标题】:how to generate an azure shared access signature for blob/container?如何为 blob/容器生成 azure 共享访问签名?
【发布时间】:2019-10-10 22:00:26
【问题描述】:

鉴于我的环境限制以及 SAS 似乎是一个简单的令牌,我想“手动”生成它。

我尝试关注微软的documentation SAS是如何生成的,但总是收到错误:

curl.exe -X PUT -T .\arckep2.jpg -H "Content-Type: image/jpeg"  -H "x-ms-date: $now" -H "x-ms-blo
b-type: BlockBlob" "http://127.0.0.1:10000/devstoreaccount1/profile-images/arckep3.jpg?sv=2018-03-28&sr=c&sig=2%2FKJVDhs2O5%2F5nAGpGzxRhnN4PE4AqPHOe3fFe7qC7o%3D&st=2019
-05-23T00%3A00%3A00Z&se=2019-05-25T00%3A00%3A00Z&sp=rwdl"

返回:

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:d1313189-cde5-463d-8476-1aab15a9f03d
Time:2019-05-24T09:20:16.1975584Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was rwdl
2019-05-23T00:00:00Z
2019-05-25T00:00:00Z
/blob/devstoreaccount1/profile-images



2018-03-28




</AuthenticationErrorDetail></Error>

这有什么问题?

我尝试在 codepen 中复制 azure-sdk-for-jshttps://codepen.io/nagyv/pen/MdVpgX

const secret = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="  // Secret for every Azure storage emulator

function getParts() {
  return [
    "rwdl",
    "2019-05-23T00:00:00Z",
    "2019-05-25T00:00:00Z",
    "/blob/devstoreaccount1/profile-images",
    "",
    "",
    "",
    "2018-03-28",
    "",
    "",
    "",
    "",
    ""
  ].join("\n")
}
const parts = getParts()

function sign(message) {
  const crypted = CryptoJS.HmacSHA256(message, secret);
  return CryptoJS.enc.Base64.stringify(crypted)
}

const content = document.getElementById('content')
content.innerHTML = encodeURIComponent(sign(parts))

上下文

我想使用基于 JavaScript 的在线无服务器后端 (GameSparks) 为我的基于浏览器的应用程序生成 SAS。不幸的是,考虑到后端,我不能使用 azure node SDKs 来生成 SAS。由于 SAS 似乎是一个简单的令牌,我想“手动”生成它。

【问题讨论】:

    标签: javascript azure azure-storage azure-storage-emulator


    【解决方案1】:

    经过一番处理,我意识到问题在于,在 Azure 的解决方案中,秘密被 base64 解码。

    这显示了如何生成相同的摘要:https://stackoverflow.com/a/56295850/245493

    这个改成

    function sign(message) {
      const crypted = CryptoJS.HmacSHA256(message, CryptoJS.enc.Base64.parse(secret))
      return CryptoJS.enc.Base64.stringify(crypted)
    }
    

    解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2013-12-31
      • 2014-02-08
      • 2018-09-02
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      相关资源
      最近更新 更多