【问题标题】:Download a zip file from azure blob storage using R使用 R 从 azure blob 存储下载 zip 文件
【发布时间】:2018-01-09 04:02:01
【问题描述】:

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

【问题讨论】:

    标签: r zipfile azure-blob-storage


    【解决方案1】:

    我设法使用此代码轻松下载任何文件

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 2015-06-20
      • 2021-12-08
      • 2021-07-15
      • 2021-08-09
      • 2020-08-08
      • 2012-02-04
      相关资源
      最近更新 更多