簡體   English   中英

通過https使用Azure Blob存儲REST api

[英]Using Azure Blob storage REST api with https

我正在嘗試針對azure存儲API向不公開可見的帳戶提出請求,該請求需要經過身份驗證。

我試圖按照此頁面的標題進行操作: https : //msdn.microsoft.com/zh-cn/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進行訪問?

嘗試使用http://storageexplorer.com/工具訪問blob,看看是否可以訪問blob。

這是一個提供示例代碼的線程,該示例代碼如何使用SAS /帳戶名稱為REST API構造Authorization標頭。
Azure-調用Storage Rest API以獲得列表Blob

有沒有理由不使用為您執行此邏輯的Storage SDK。 我們提供所有主要語言的語言-請參閱本入門指南頂部的標簽列表。 或者,我們在GitHub上提供了這些庫的所有源代碼(例如-這是.NET源代碼 ),您可以在源代碼內看到簽名邏輯-簽出SAS令牌的SharedAccessSignatureHelper( 在此處 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM