2016-09-30 180 views
0

我正在尝试针对Azure存储API请求一个不公开的帐户,并且需要进行身份验证。与https使用Azure Blob存储REST api

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

我根本无法进行这项工作。我总是收到一个“ResourceNotFound”错误,我无法解释这个错误,因为我绝对不会忘记存储帐户或容器名称。我还使用相同的帐户,容器和密钥成功连接到Power BI。

我唯一能想到的可能是签名的生成,我可能会在编码中丢失(第一次我正在做这样的事情)。但是这并不能解释错误消息是“ResourceNotFound”。以下是请求(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访问?

+0

因此,为了演示的目的,MS是否提供了公开可用的验证密钥的虚拟帐户?否则,你应该去一些特定于Azure的论坛,因为剩下的我们非Azure用户将无法测试任何答案。 –

+0

我希望这不是你真正的SharedKey – Paparazzi

+0

尝试将容器放在路径中。例如http:// .blob.core.windows.net/mycontainer – Paparazzi

回答

0

是不是有一个原因,你没有使用存储SDK的执行这个逻辑给你。我们拥有所有主要语言 - 请参阅此Getting Started指南顶部的选项卡列表。或者,我们可以在GitHub上获得这些库的所有源代码(例如 - 这里是。NET source code),您可以在源代码中看到签名逻辑 - 查看SharedAccessSignatureHelper获取SAS令牌(here)。

+0

我想在R中这样做,因为我将数据导入Power BI,它直接与R集成。只要我有一些时间再看一遍,我会通过你和Gunjan分享的链接看看他们能否帮助我。谢谢 – Zepee