【问题标题】:Authentication failure @ Azure node js generating SAS. Signature did not match. String to sign used was w身份验证失败@Azure 节点 js 生成 SAS。签名不匹配。使用的签名字符串是 w
【发布时间】:2016-06-16 21:20:33
【问题描述】:

我正在尝试使用 SAS 将图像上传到 Azure 存储。这是我用来生成 SAS 的代码。

function getWriteSAS() {
    var blobService = storage.createBlobService();
    var container = 'container-name';
    var blobSAS = blobService.generateSharedAccessSignature(container, mCurrentRequest.param.blobName, getSharedAccessPolicy(10));
    urlForDownloading = blobService.host.primaryHost + container + '?' + blobSAS
    mCurrentResponse.status(200).send({"SASURI" : urlForDownloading})
}

function getSharedAccessPolicy(accessTimeInMinutes) {
    var startDate = new Date();
    var expiryDate = new Date(startDate);
    expiryDate.setMinutes(startDate.getMinutes() + accessTimeInMinutes);
    startDate.setMinutes(startDate.getMinutes() - accessTimeInMinutes);

    var sharedAccessPolicy = {
        AccessPolicy: {
            Permissions: storage.BlobUtilities.SharedAccessPermissions.WRITE,
            Start: startDate,
            Expiry: expiryDate
        },
    };
    return sharedAccessPolicy;
}

创建的 URL 看起来像这样: https://irewardchart.blob.core.windows.net/container-name?st=2016-06-16T06%3A24%3A52Z&se=2016-06-16T06%3A44%3A52Z&sp=w&sv=2015-04-05&sr=c&sig=c%2F%2B7AtkH7zwLhzF%2B74%2FUeMeQ4eLDnWvVDMkiqSqroqc%3D

当我尝试从浏览器点击 URL 时,我得到了这个结果。

<Error>
<script/>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:2fab1644-0001-0087-7c99-c77b1a000000 Time:2016-06-16T06:36:14.5020064Z
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was w 2016-06-16T06:24:52Z 2016-06-16T06:44:52Z /blob/irewardchart/$root 2015-04-05
</AuthenticationErrorDetail>
</Error>

我们将不胜感激。

【问题讨论】:

标签: node.js azure blob storage


【解决方案1】:

根据您的描述,您的代码生成的URL,漏掉了blobName的模式。请仔细检查应用程序中的参数mCurrentRequest.param.blobName 是否得到正确的值。

顺便说一下,还有两点你可以注意:

  1. 如果你的需求是在浏览器中浏览blob内容,我建议你使用storage.BlobUtilities.SharedAccessPermissions.READ权限,这样可以减少权限问题。
  2. 由于 Azure-storage-node sdk 提供了一个可以生成带有 SAS 令牌 getUrl() 的 blob url 的功能。因此,如果您检查了mCurrentRequest.param.blobName 的值正确但仍然出现问题,请尝试以下代码:

    ...
    var blobSAS = blobService.generateSharedAccessSignature(<containerName>, <blobName>, getSharedAccessPolicy(10));
    var sharedBlobService = azure.createBlobServiceWithSas(blobService.host, blobSAS);
    urlForDownloading =  blobService.getUrl(container,blobName,blobSAS)
    ...
    

【讨论】:

    猜你喜欢
    • 2017-04-23
    • 2021-07-05
    • 1970-01-01
    • 2021-06-09
    • 2020-05-29
    • 2017-07-24
    • 1970-01-01
    • 2015-03-26
    • 2018-10-02
    相关资源
    最近更新 更多