當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R googledrive drive_get 按路徑或 ID 獲取雲端硬盤文件

檢索通過 path 或通過文件 id 指定的文件的元數據。如果您通過 id 指定文件,則此函數非常簡單。但是,當您通過 path 指定目標文件時,有一些重要的注意事項。請參閱下文了解更多信息。如果通過 path 指定目標文件,則返回的 dribble 將包含 path 列。

用法

drive_get(
  path = NULL,
  id = NULL,
  shared_drive = NULL,
  corpus = NULL,
  verbose = deprecated(),
  team_drive = deprecated()
)

參數

path

要獲取的路徑的字符向量。使用尾部斜杠明確指示路徑是文件夾,如果存在同名文件,可以消除歧義(是的,這在雲端硬盤上是可能的!)。如果 path 似乎包含雲端硬盤 URL 或顯式標記為 as_id() ,則將其視為通過 id 參數提供。

id

雲端硬盤文件 ID 或 URL 的字符向量(首先使用 as_id() 進行處理)。如果 pathid 都不是 NULL ,則 id 將被靜默忽略。

shared_drive

標識一個特定共享驅動器的任何內容:其名稱、其 ID 或標有 as_id()dribble 的 URL。提供給 shared_drive 的值使用 as_shared_drive() 進行預處理。了解更多關於shared drives的信息。

corpus

字符,指定要搜索的項目集合。與使用共享雲端硬盤和/或 Google Workspace 網域的人員相關。如果指定,則必須是 "user""drive" (要求還指定 shared_drive )、 "allDrives""domain" 之一。了解更多關於shared drives的信息。

verbose

[Deprecated]這種對各個 googledrive 函數的邏輯論證已被棄用。要全局禁止 googledrive 消息傳遞,請使用options(googledrive_quiet = TRUE)(默認行為是發出信息性消息)。要以更有限的方式抑製消息傳遞,請使用幫助程序local_drive_quiet()或者with_drive_quiet().

team_drive

Google Drive 和 Drive API 已用共享雲端硬盤取代了團隊雲端硬盤。

dribble 類的對象,每個文件一行的 tibble。如果通過 path 指定目標文件,則會有 path 列。

通過獲取path

Google Drive 的行為方式與您的本地文件係統不同!文件和文件夾名稱不必是唯一的,即使在給定的層次結構級別也是如此。這意味著單個路徑可以說明多個文件(或 0 個或恰好 1 個)。

單個文件還可以與多個路徑兼容,即一個路徑可以比另一個路徑更具體。位於 ~/alfa/bravo 的文件可以作為 bravoalfa/bravo~/alfa/bravo 找到。如果所有這 3 個都包含在輸入 path 中,則它們將在輸出中由一行表示。

使用文件路徑時,最好將 drive_get() 視為設置操作。不要假設第 i 輸入路徑對應於輸出中的行 i(盡管經常如此!)。如果輸入和輸出之間不存在一對一的關係,則會在消息中宣布。

drive_get() 執行足夠的路徑解析來唯一標識與每個輸入 path 兼容的文件,對於所有 path 一次。如果您絕對想要完整的規範路徑,請運行 drive_get()drive_reveal(d, "path") ` 的輸出。

不屬於您的文件

如果您想通過 path 獲取文件,並且該文件不一定位於您的“我的雲端硬盤”上,則您可能需要指定 shared_drivecorpus 參數來搜索其他項目集合。了解更多關於shared drives的信息。

也可以看看

要將路徑信息添加到任何缺少路徑信息的 dribble,請使用 drive_reveal(d, "path") 。要列出文件夾的內容,請使用 drive_ls() 。對於一般搜索,請使用 drive_find()

包裝 files.get 端點,如果您按名稱或路徑指定文件,還會調用 files.list

例子

# get info about your "My Drive" root folder
drive_get("~/")
#> ✔ The input `path` resolved to exactly 1 file.
#> # A dribble: 1 × 4
#>   name     path  id                  drive_resource   
#>   <chr>    <chr> <drv_id>            <list>           
#> 1 My Drive ~/    0AO_RMaBzcP63Uk9PVA <named list [31]>
# the API reserves the file id "root" for your root folder
drive_get(id = "root")
#> # A dribble: 1 × 3
#>   name     id                  drive_resource   
#>   <chr>    <drv_id>            <list>           
#> 1 My Drive 0AO_RMaBzcP63Uk9PVA <named list [31]>
drive_get(id = "root") %>% drive_reveal("path")
#> # A dribble: 1 × 4
#>   name     path  id                  drive_resource   
#>   <chr>    <chr> <drv_id>            <list>           
#> 1 My Drive ~/    0AO_RMaBzcP63Uk9PVA <named list [31]>

# set up some files to get by path
alfalfa <- drive_mkdir("alfalfa")
#> Created Drive file:
#> • alfalfa <id: 1ihWThx8L2kRtnhLAIuN9fX2QV1_qnrdR>
#> With MIME type:
#> • application/vnd.google-apps.folder
broccoli <- drive_upload(
  drive_example_local("chicken.txt"),
  name = "broccoli", path = alfalfa
)
#> Local file:
#> • /home/runner/work/_temp/Library/googledrive/extdata/example_files/chicken.txt
#> Uploaded into Drive file:
#> • broccoli <id: 191yJH2qNolW8EG5dtnny-P4AgVOCV_q->
#> With MIME type:
#> • text/plain
drive_get("broccoli")
#> ! Problem with 1 path: path is compatible with more than 1 file
#>   broccoli
#> ! No path resolved to exactly 1 file.
#> # A dribble: 2 × 4
#>   name     path     id                                drive_resource   
#>   <chr>    <chr>    <drv_id>                          <list>           
#> 1 broccoli broccoli 191yJH2qNolW8EG5dtnny-P4AgVOCV_q- <named list [41]>
#> 2 broccoli broccoli 1aNh9_YiunRSwgmopO5hZJgusiZQh1Ii7 <named list [42]>
drive_get("alfalfa/broccoli")
#> ✔ The input `path` resolved to exactly 1 file.
#> # A dribble: 1 × 4
#>   name     path               id       drive_resource   
#>   <chr>    <chr>              <drv_id> <list>           
#> 1 broccoli ~/alfalfa/broccoli 191yJH2… <named list [41]>
drive_get("~/alfalfa/broccoli")
#> ✔ The input `path` resolved to exactly 1 file.
#> # A dribble: 1 × 4
#>   name     path               id       drive_resource   
#>   <chr>    <chr>              <drv_id> <list>           
#> 1 broccoli ~/alfalfa/broccoli 191yJH2… <named list [42]>
drive_get(c("broccoli", "alfalfa/", "~/alfalfa/broccoli"))
#> ! Problem with 1 path: path is compatible with more than 1 file
#>   broccoli
#> ! 1 file in the output is associated with more than 1 input `path`
#>   broccoli <id: 191yJH2qNolW8EG5dtnny-P4AgVOCV_q->
#> ! 2 out of 3 input paths resolved to exactly 1 file.
#> # A dribble: 3 × 4
#>   name     path               id       drive_resource   
#>   <chr>    <chr>              <drv_id> <list>           
#> 1 broccoli ~/alfalfa/broccoli 191yJH2… <named list [42]>
#> 2 broccoli ~/broccoli         1aNh9_Y… <named list [42]>
#> 3 alfalfa  ~/alfalfa/         1ihWThx… <named list [33]>

# Clean up
drive_rm(alfalfa)
#> File deleted:
#> • alfalfa <id: 1ihWThx8L2kRtnhLAIuN9fX2QV1_qnrdR>

if (FALSE) {
# The examples below are indicative of correct syntax.
# But note these will generally result in an error or a
# 0-row dribble, unless you replace the inputs with paths
# or file ids that exist in your Drive.

# multiple names
drive_get(c("abc", "def"))

# multiple names, one of which must be a folder
drive_get(c("abc", "def/"))

# query by file id(s)
drive_get(id = "abcdefgeh123456789")
drive_get(as_id("abcdefgeh123456789"))
drive_get(id = c("abcdefgh123456789", "jklmnopq123456789"))

# apply to a browser URL for, e.g., a Google Sheet
my_url <- "https://docs.google.com/spreadsheets/d/FILE_ID/edit#gid=SHEET_ID"
drive_get(my_url)
drive_get(as_id(my_url))
drive_get(id = my_url)

# access the shared drive named "foo"
# shared_drive params must be specified if getting by path
foo <- shared_drive_get("foo")
drive_get(c("this.jpg", "that-file"), shared_drive = foo)
# shared_drive params are not necessary if getting by id
drive_get(as_id("123456789"))

# search all shared drives and other files user has accessed
drive_get(c("this.jpg", "that-file"), corpus = "allDrives")
}
源代碼:R/drive_get.R

相關用法


注:本文由純淨天空篩選整理自Jennifer Bryan等大神的英文原創作品 Get Drive files by path or id。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。