使用R从azure blob存储中下载zip文件

时间:2017-08-02 05:29:32

标签: r zipfile azure-blob-storage

我想从azure blob存储中下载一个zip文件,但我得到的输出" azureBlobCall"不是zip文件。我不知道如何指定内容类型以及如何管理在R中获取正确的格式。

1 个答案:

答案 0 :(得分:0)

我可以使用此代码轻松下载任何文件

library(httr)
library(RCurl)
library(xml2)

account #Account Name
container #container name
key #primary key
file #file to be downloaded
filepath #foldername for results to be downloaded

DownloadFromAZureBlob<-function(account,container,key,file,filepath){

  verb="GET"

  url <- paste("https://",account,".blob.core.windows.net/",container,'/',basename(file),sep="")

  container <- paste(container,'/',basename(file),sep = "")

  # combine timestamp and version headers with any input headers, order and create the CanonicalizedHeaders
  `x-ms-date` <- format(Sys.time(),"%a, %d %b %Y %H:%M:%S %Z", tz="GMT")
  headers <- setNames(c(`x-ms-date`, "2015-04-05"), 
                      c("x-ms-date", "x-ms-version"))
  headers <- headers[order(names(headers))]

  CanonicalizedHeaders <- paste(names(headers), headers, sep=":", collapse = "\n")

  CanonicalizedResource <- paste0("/",account,"/",container)

  # create the authorizationtoken
  signaturestring <- paste0(verb, "\n\n\n\n\n\n\n\n\n\n\n\n", CanonicalizedHeaders, "\n", CanonicalizedResource)
  requestspecificencodedkey <- RCurl::base64(
    digest::hmac(key=RCurl::base64Decode(key, mode="raw"),
                 object=enc2utf8(signaturestring),
                 algo= "sha256", raw=TRUE)
  )

  authorizationtoken <- paste0("SharedKey ", account, ":", requestspecificencodedkey)
  # make the call
  headers_final <- add_headers(Authorization=authorizationtoken, headers)
  #download to subdirectry "filepath"
  download.file(url,destfile=file.path(filepath, basename(file)), method='auto',extra =headers_final )


}

DownloadFromAZureBlob(account,container,key,file,filepath)
相关问题