首页 文章

从RStudio中的Google Cloud 存储访问文件

提问于
浏览
1

我一直在尝试在谷歌 Cloud 存储和RStudio服务器(我在谷歌 Cloud 中调整的那个)之间 Build 连接,这样我就可以访问R中的文件来运行总和分析了 . 我已经在网上找到了三种不同的方法,但到目前为止我还没有看到很多关于这些方法的清晰度 .

  • 使用特定于文件的公共URL访问该文件[这不是我的选项]

  • 将Google Cloud 存储作为光盘安装在RStudio服务器中,并像访问服务器中的任何其他文件一样访问它[我看到有人发布了有关此方法的信息,但无法找到显示其完成方式的任何指南或材料]

  • 使用googleCloudStorageR包可以完全访问Cloud Storage存储桶 .

第3步看起来像是非常标准的方法 . 但是当我尝试点击gcs_auth()命令时出现以下错误

gar_auto_auth中的错误(required_scopes,new_user = new_user,no_auto = no_auto,:无法进行身份验证 - 需要将选项(googleAuthR.scopes.selected)设置为包含https://www.googleapis.com/auth/devstorage.full_control或https: //www.googleapis.com/auth/devstorage.read_write或https://www.googleapis.com/auth/cloud-platform

关于如何使用它进行连接的指南可以在https://github.com/cloudyr/googleCloudStorageR上找到,但是它说它需要一个service-auth.json文件来设置环境变量和所有其他密钥和密钥,但是并没有真正指定这些是什么 .

如果有人可以帮我知道这是如何设置的,或者指出一个关于设置环境的好指南,我将非常感激 .

谢谢 .

2 回答

  • 1

    您可能需要FUSE适配器 - 这将允许您将GCS存储桶安装为服务器上的目录 .

    • 在R服务器上安装gcsfuse .

    • 创建一个mnt目录 .

    • 运行gcsfuse your-bucket / path / to / mnt

    请注意,虽然RW性能对FUSE来说并不是很好

    完整的文档

    https://cloud.google.com/storage/docs/gcs-fuse

  • 1

    在使用Google Cloud 端的任何服务之前,您必须附上您的卡 .
    所以,我假设您创建了帐户,创建帐户后转到 Console ,如果您尚未创建 Project 然后 Create Project ,则单击侧栏找到 APIs & Services > Credentials .
    然后,
    1) Create Service Account Keys 在json中保存此文件,您只能下载一次 .
    2) OAuth 2.0 client ID 给出应用程序的名称并选择类型作为Web应用程序并下载json文件 .

    现在,对于存储,请转到侧边栏查找存储并单击它 .
    创建Bucket并提供Bucket的名称 .
    我在存储桶中添加了单个图像,您也可以添加代码用途 .

    让我们看看如何从存储中下载此图像,以便您可以按照您提供的链接进行其他操作 .

    首先将环境文件创建为.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
    

相关问题