简体   繁体   中英

Download a zip file from azure blob storage using R

I want to download a zip file from azure blob storage but the output I get using "azureBlobCall" is not a zip file. I don't know how to specify the content type and how to manage getting the correct format in R.

I manged to download any file easily with this code

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM