我想 (i) 使用 RStudio 列出位于 ADLSgen2 存储帐户容器中的特定目录的内容,以及 (ii) 读取以 DataFrame 形式存储在该目录中的 csv。
需要注意的是,我只有对相关特定目录的读取访问权限(通过 Azure AD 组和 ACL 的组合)
library("AzureStor")
library("readr")
library("AzureAuth")
tenant_id <- "hidden"
client_id <- "hidden"
client_secret <- "hidden"
storage_account <- "hidden"
container_name <- "hidden"
directory_path <- "hidden"
file_path <- "hidden"
auth <- AzureAuth::get_azure_token(
resource = "https://storage.azure.com",
tenant = tenant_id,
app = client_id,
password = client_secret
)
adls <- adls_endpoint(
endpoint = sprintf("https://%s.dfs.core.windows.net", storage_account),
token = auth
)
directory_contents <- list_storage_containers(adls, container_name, directory_path)
print(directory_contents) # I need access to the entire ADLS to run this
cont <- storage_container(adls, container_name)
list_adls_files(cont) # I need access to the entire container to run this
问题是,使用“list_storage_containers”可以列出 ADLS 的容器,而使用“storage_container”可以列出容器中的目录。
我实际上想列出相关容器中目录的内容,并读取该目录作为数据帧的 csv(可能使用“dask”库)
这里是 AzureStor 的文档:https://cran.r-project.org/web/packages/AzureStor/vignettes/generics.html
您可以使用以下代码通过 Azure AD 身份验证从特定目录读取内容列表:
library(AzureStor)
token <- AzureRMR::get_azure_token("https://storage.azure.com",
tenant="<tenantId>", app="<clientId>", password="<clientSecret>")
ad_endp_tok <- storage_endpoint("https://<ADLSName>.dfs.core.windows.net", token=token)
cont <- storage_container(ad_endp_tok, "<containerName>")
list_storage_files(cont,"<directory>")
您可以使用以下代码从指定目录读取csv文件:
storage_download(cont, "inputs/input.csv", "in.csv")
df = read.csv("in.csv")