首页 > 解决方案 > 如何使用 R 从共享点文件夹下载文件

问题描述

我公司的 sharepint 文件夹中有一些文件。我需要使用 r 下载该文件夹中的所有文件。我怎么能用 R 做到这一点?

我用谷歌搜索了这个问题并得到了这个链接。这是关于将文件上传到共享点文件夹。[从 R 将文件上传到 SharePoint

链接中的代码如下所示:

saveToSharePoint <- function(fileName) 
  {
   cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
              "--upload-file /home/username/FolderNameWhereTheFileToTransferExists/",fileName, 
              "teamsites.OrganizationName.com/sites/PageTitle/Documents/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
   system(cmd)
  }

 saveToSharePoint("SomeFileName.Ext")

这是关于上传一个特定的文件。但我需要的是从共享点文件夹下载文件。

所以,我修改了从sharepoint文件夹复制的代码。我--upload-file改为--download-file.

copyFromSharePoint <- function(fileName) 
      {
       cmd <- paste("curl --max-time 7200 --connect-timeout 7200 --ntlm --user","username:password", 
                  "--download-file teamsites.OrganizationName.com/sites/PageTitle/Documents/FolderNameWhereTheFileToTransferExists/",fileName, 
                  "home/username/UserDocumentation/FolderNameWhereTheFileNeedsToBeCopied/",fileName, sep = " ")
       system(cmd)
      }

copyFromSharePoint("SomeFileName.Ext")

但是,这似乎不起作用。下面的错误信息:

curl: option --download-file: is unknown

有谁知道我怎么能用 R 做到这一点?

另外,如果我需要下载文件夹中的所有文件,而不是一个特定的文件怎么办。有谁知道我如何用 R 来做到这一点?

标签: rcurldownloadrcurl

解决方案


解决方案

我制作了一个名为sharepointr的简单 R 包,用于从 SharePoint 上传和下载文件。

我从 R 完成整个下载/上传到共享点工作的方式是:

  1. 进行 SharePoint 应用程序注册
  2. 为应用注册添加权限
  3. 使用 cURL 获取“tenant_id”和“resource_id”
  4. 对 API 进行 get/post 调用

它都在包Readme.md中描述

例子

# Installing the package
install.packages("devtools")
devtools::install_github("esbeneickhardt/sharepointr")

# Setting parameters
client_id <- "insert_from_first_step"
client_secret <- "insert_from_first_step"
tenant_id <- "insert_from_fourth_step"
resource_id <- "insert_from_fourth_step"
site_domain <- "yourorganisation.sharepoint.com"
sharepoint_url <- "https://yourorganisation.sharepoint.com/sites/MyTestSite"

# Getting a SharePoint Token
sharepoint_token <- get_sharepoint_token(client_id, client_secret, tenant_id, resource_id, site_domain)

# Getting sharepoint digest value
sharepoint_digest_value <- get_sharepoint_digest_value(sharepoint_token, sharepoint_url)

# Downloading a file
sharepoint_path <- "Shared Documents/test"
sharepoint_file_name <- "Mappe.xlsx"
out_path <- "C:/Users/User/Desktop/"
download_sharepoint_file(sharepoint_token, sharepoint_url, sharepoint_digest_value, sharepoint_path, sharepoint_file_name, out_path)

推荐阅读