2017-08-02 78 views
0

從azure blob存儲下載zip文件我想從azure blob存儲中下載zip文件,但使用「azureBlobCall」的輸出不是zip文件。我不知道如何指定內容類型以及如何管理得到正確格式R.使用R

回答

0

我manged與此代碼

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) 
輕鬆下載任何文件