在 RStudio 中访问来自 Google 云存储的文件
Accessing files from Google cloud storage in RStudio
我一直在尝试在 Google 云存储和 RStudio 服务器(我在 Google 云中启动的服务器)之间建立连接,以便我可以访问 R 中的文件到 运行求和分析上。
我在网上找到了三种不同的方法,但到目前为止我还没有看到这些方法的清晰度。
- 使用特定于文件的 public URL 访问文件 [这不是我的选择]
- 将 Google 云存储作为光盘安装在 RStudio 服务器中,并像访问服务器中的任何其他文件一样访问它 [我看到有人 post 关于此方法,但在任何指南或显示它是如何完成的材料]
- 使用 googleCloudStorageR 包获得对云存储桶的完全访问权限。
第 3 步看起来是非常标准的方法。但是当我尝试点击 gcs_auth() 命令
时出现以下错误
Error in gar_auto_auth(required_scopes, new_user = new_user, no_auto =
no_auto, : Cannot authenticate -
options(googleAuthR.scopes.selected) needs to be set to
includehttps://www.googleapis.com/auth/devstorage.full_control or
https://www.googleapis.com/auth/devstorage.read_write or
https://www.googleapis.com/auth/cloud-platform
有关如何使用此连接的指南可在
https://github.com/cloudyr/googleCloudStorageR
但它说它需要一个服务-auth.json 文件来设置环境变量和所有其他密钥和秘密密钥,但并没有真正说明这些是什么。
如果有人可以帮助我了解这实际上是如何设置的,或者为我提供有关设置环境的好指南,我将不胜感激。
谢谢。
您可能需要 FUSE 适配器 - 这将允许您将 GCS 存储桶作为目录安装在服务器上。
- 在 R 服务器上安装 gcsfuse。
- 创建 mnt 目录。
- 运行 gcsfuse 你的桶 /path/to/mnt
请注意,RW 性能与 FUSE 相比并不出色
完整文档
在使用 google 云提供的任何服务之前,您必须附上您的卡。
因此,我假设您已经创建了帐户,在创建帐户后转到 Console ,如果您还没有创建 Project 然后 创建项目,然后点击边栏找到APIs & Services > Credentials.
那么,
1)创建服务帐户密钥将此文件保存在json中,您只能下载一次。
2)OAuth 2.0 客户端 ID 给出应用程序的名称,select 输入 Web 应用程序并下载 json 文件。
现在,对于存储,转到边栏中的“查找存储”并单击它。
创建 Bucket 并给 Bucket 命名。
我已经在桶中添加了单个图像,您也可以添加用于代码目的。
让我们看看如何从存储中下载此图像以进行其他操作,您可以按照您提供的 link 进行操作。
首先将环境文件创建为 .Renviron,以便它自动捕获 json 文件并将其保存在工作目录中。
In .Renviron file add those two downloaded json files like this
GCS_AUTH_FILE="serviceaccount.json"
GAR_CLIENT_WEB_JSON="Oauthclient.json"
#R part
library(googleCloudStorageR)
library(googleAuthR)
gcs_auth() # for authentication
#set the scope
gar_set_client(scopes = c("https://www.googleapis.com/auth/devstorage.read_write",
"https://www.googleapis.com/auth/cloud-platform"))
gcs_get_bucket("you_bucket_name") #name of the bucket that you have created
gcs_global_bucket("you_bucket_name") #set it as global bucket
gcs_get_global_bucket() #check if your bucket is set as global,you should get your bucket name
objects <- gcs_list_objects() # data from the bucket as list
names(objects)
gcs_get_object(objects$name[[1]], saveToDisk = "abc.jpeg") #save the data
**Note :**if you dont get json file loaded restart the session using .rs.restartR()
and check the using
Sys.getenv("GCS_AUTH_FILE")
Sys.getenv("GAR_CLIENT_WEB_JSON")
#it should show the files
我一直在尝试在 Google 云存储和 RStudio 服务器(我在 Google 云中启动的服务器)之间建立连接,以便我可以访问 R 中的文件到 运行求和分析上。 我在网上找到了三种不同的方法,但到目前为止我还没有看到这些方法的清晰度。
- 使用特定于文件的 public URL 访问文件 [这不是我的选择]
- 将 Google 云存储作为光盘安装在 RStudio 服务器中,并像访问服务器中的任何其他文件一样访问它 [我看到有人 post 关于此方法,但在任何指南或显示它是如何完成的材料]
- 使用 googleCloudStorageR 包获得对云存储桶的完全访问权限。
第 3 步看起来是非常标准的方法。但是当我尝试点击 gcs_auth() 命令
时出现以下错误Error in gar_auto_auth(required_scopes, new_user = new_user, no_auto = no_auto, : Cannot authenticate - options(googleAuthR.scopes.selected) needs to be set to includehttps://www.googleapis.com/auth/devstorage.full_control or https://www.googleapis.com/auth/devstorage.read_write or https://www.googleapis.com/auth/cloud-platform
有关如何使用此连接的指南可在 https://github.com/cloudyr/googleCloudStorageR 但它说它需要一个服务-auth.json 文件来设置环境变量和所有其他密钥和秘密密钥,但并没有真正说明这些是什么。
如果有人可以帮助我了解这实际上是如何设置的,或者为我提供有关设置环境的好指南,我将不胜感激。
谢谢。
您可能需要 FUSE 适配器 - 这将允许您将 GCS 存储桶作为目录安装在服务器上。
- 在 R 服务器上安装 gcsfuse。
- 创建 mnt 目录。
- 运行 gcsfuse 你的桶 /path/to/mnt
请注意,RW 性能与 FUSE 相比并不出色
完整文档
在使用 google 云提供的任何服务之前,您必须附上您的卡。
因此,我假设您已经创建了帐户,在创建帐户后转到 Console ,如果您还没有创建 Project 然后 创建项目,然后点击边栏找到APIs & Services > Credentials.
那么,
1)创建服务帐户密钥将此文件保存在json中,您只能下载一次。
2)OAuth 2.0 客户端 ID 给出应用程序的名称,select 输入 Web 应用程序并下载 json 文件。
现在,对于存储,转到边栏中的“查找存储”并单击它。
创建 Bucket 并给 Bucket 命名。
我已经在桶中添加了单个图像,您也可以添加用于代码目的。
让我们看看如何从存储中下载此图像以进行其他操作,您可以按照您提供的 link 进行操作。
首先将环境文件创建为 .Renviron,以便它自动捕获 json 文件并将其保存在工作目录中。
In .Renviron file add those two downloaded json files like this
GCS_AUTH_FILE="serviceaccount.json"
GAR_CLIENT_WEB_JSON="Oauthclient.json"
#R part
library(googleCloudStorageR)
library(googleAuthR)
gcs_auth() # for authentication
#set the scope
gar_set_client(scopes = c("https://www.googleapis.com/auth/devstorage.read_write",
"https://www.googleapis.com/auth/cloud-platform"))
gcs_get_bucket("you_bucket_name") #name of the bucket that you have created
gcs_global_bucket("you_bucket_name") #set it as global bucket
gcs_get_global_bucket() #check if your bucket is set as global,you should get your bucket name
objects <- gcs_list_objects() # data from the bucket as list
names(objects)
gcs_get_object(objects$name[[1]], saveToDisk = "abc.jpeg") #save the data
**Note :**if you dont get json file loaded restart the session using .rs.restartR()
and check the using
Sys.getenv("GCS_AUTH_FILE")
Sys.getenv("GAR_CLIENT_WEB_JSON")
#it should show the files