【问题标题】:Using Azure Blob storage REST api with https将 Azure Blob 存储 REST api 与 https 结合使用
【发布时间】:2017-02-09 06:51:35
【问题描述】:

我正在尝试针对 azure 存储 API 向不公开可见的帐户发出请求,并且需要对请求进行身份验证。

我试图关注此页面的标题: https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

我根本无法完成这项工作。我总是收到“ResourceNotFound”错误,我无法解释,因为我绝对没有输入错误的存储帐户或容器名称。我还使用相同的帐户、容器和密钥成功连接到 Power BI。

我唯一能想到的可能是签名的生成,我可能会迷失在编码中(我第一次做这样的事情)..但这并不能解释错误消息是“资源未找到”。这是请求(R)的代码:

#azure storage endpoint to hit against
account <- "myaccount"
container <- "mycontainer"
requestProperties <- "comp=list"
endPoint <- paste("https://", account, ".blob.core.windows.net/", sep = "")
endPoint
#[1] "https://myaccount.blob.core.windows.net/"


#date header
timeStamp <- Sys.time()
timeString <- format(timeStamp, format="%y-%m-%d %H:%M:%S", tz="GMT", usetz = TRUE)
timeString <- "Fri, 30 Sep 2016 14:54:30 GMT"
dateHeader <- paste("x-ms-date", timeString, sep = ":")
dateHeader
#[1] "x-ms-date:Fri, 30 Sep 2016 14:54:30 GMT"

#version header
versionHeader <- "x-ms-version:2015-02-21"

#authorization header
requestVerb <- "GET"
authType <- "SharedKey"
azureKey <- "myAccountKey"

newLines <- "\n\n\n\n\n\n\n\n\n\n"
canonicalizedHeaders <- paste(dateHeader,versionHeader, sep = "\n")

#build canonicalized resource
resourceAccount <- paste("/",account, sep = "")
resourceContainer <- paste ("/",container, sep = "")
resource <- paste(resourceAccount, resourceContainer, sep = " ")

canonicalizedResource <- paste(resource, requestProperties, sep = "\n")
canonicalizedResource
#[1] "/myaccount /mycontainer\ncomp=list"

#build authentication signed string
stringToSign <- paste(requestVerb, newLines, canonicalizedHeaders, canonicalizedResource, sep = "\n")
stringToSign <- enc2utf8(stringToSign)
stringToSign
#[1] "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:Fri, 30 Sep 2016 14:54:30 GMT\nx-ms-version:2015-02-21\n/myaccount /mycontainer\ncomp=list"

Signature  <- digest::hmac(object = stringToSign, key = azureKey, algo = "sha256", serialize = FALSE)

#authentication header
authorization <- paste(account, Signature, sep = ":")
authorization
#[1] "myaccount:b4761595ea09d0e9d56223bd0a14233bca2b9fc3bb043031586215942f5c6d06"

authHeader <- paste("Authorization:", authType, authorization, sep = " ")
authHeader
#[1] "Authorization: SharedKey myaccount:b4761595ea09d0e9d56223bd0a14233bca2b9fc3bb043031586215942f5c6d06"


#build the actual request
request <- paste(endPoint, requestProperties, sep = "?")
request
#[1] "https://myaccount.blob.core.windows.net/?comp=list"

azureRequest <- httr::GET(request, httr::add_headers(dateHeader, versionHeader, authHeader))
responseContent <- httr::content(azureRequest, as = "text")
responseContent
#[1] "<?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>ResourceNotFound</Code><Message>The specified resource does not exist.\nRequestId:b1e87500-0001-0003-6231-1b7598000000\nTime:2016-09-30T15:44:52.3452448Z</Message></Error>"

我在生成请求时是否遗漏了什么?我是否需要对我的帐户进行某些操作以允许通过 REST API 进行访问?

【问题讨论】:

  • 那么 MS 不提供一个带有公开身份验证密钥的虚拟帐户用于演示目的吗?否则你应该去一些 Azure 特定的论坛,因为我们的其他非 Azure 用户将无法测试任何答案。
  • 我希望那不是你真正的 SharedKey
  • 尝试将容器放在路径中。例如http://.blob.core.windows.net/mycontainer

标签: r rest azure azure-storage


【解决方案1】:

尝试使用 http://storageexplorer.com/ 工具访问 blob,看看是否可以访问 blob。

这是一个线程,提供了如何使用 SAS/帐户名称为 REST API 构造授权标头的示例代码。
Azure - call Storage rest api for list blobs

【讨论】:

    【解决方案2】:

    您是否有理由不使用为您执行此逻辑的存储 SDK。我们有所有主要语言的它们 - 请参阅此Getting Started 指南顶部的选项卡列表。或者,我们在 GitHub 上提供了这些库的所有源代码(例如 - 这里是 .NET source code),您可以在源代码中查看签名逻辑 - 查看 SharedAccessSignatureHelper 以获取 SAS 令牌 (here)。

    【讨论】:

    • 我想在 R 中执行此操作,因为我将数据引入 Power BI,它仅与 R 直接集成。一旦我有时间再次查看此内容,我将浏览您和 Gunjan 共享的链接,看看他们是否可以帮助我。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2013-05-25
    • 2019-11-08
    相关资源
    最近更新 更多