Accessing Google Drive from Workstations
Aims and Objectives
Many NMFS staff who will be using the Google Workstations platform require access to files and directories hosted on NOAA’s Google Drive. During this session, we’ll be covering the following topics to get you up and running with Google Workstations:
Connecting workstations to Google Drive using RClone
Direct access to Google Drive files and folders in R using the
googledrivepackage
Prerequisites: What do I need before this workshop to follow along on my own?
- This session is meant for those with a basic understanding of Google Workstations. Check out our Introduction to Google Workstations lesson if you are new to Google Workstations or need a refresher.
- We will be working on the command line, so familiarity with command line tools and operations will be helpful for this workshop.
Connecting to Google Drive using RClone
RClone is an application that provides a mountable folder of your Google Drive (and other cloud storage services) in your working directory, similar to the Google Drive for Desktop application available for Windows and Mac machines. However, unlike the Google Drive for Desktop application, RClone works in a Linux operating system, and thus will work on the Linux-based Google Workstations. Use the following steps to install and configure RClone. Note that this will work from the terminal of any of the current Workstation configurations, but for demonstration purposes I will use the RStudio workstation here.
Create or launch an RStudio workstation in your Google Cloud Console

In your workstation RStudio terminal, run the following script to set up RClone. This will start a configuration process in the terminal. After every prompt, be sure to hit
Enterafter you enter the appropriate response.sudo apt update # NOTE: # The command below installs an older version of rclone, # which does NOT provide the config_token option later. sudo apt install fuse3 rclone # Install the latest version of rclone sudo curl https://rclone.org/install.sh | sudo bash mkdir ~/gdrive rclone config
When prompted, enter
Yto finish installing the proper packages, thennto make a New remote. Next, enter the name ofgdrive.



In the next prompt for Storage, enter
24and hit enter.

Leave the client_id and client_secret prompts blank (just enter through them). Enter
1for scope to grant full read-write permissions to your Google Drive.

Enter through the service_account_file step without entering a value, and enter
nwhen prompted to Edit advanced config?. Also enternwhen prompted to use auto config.

Here is where things get a little tricky. You will also need to have the same version of RClone installed on your local machine. To do this on a Mac or a Windows machine, go to Rclone downloads and download the current version for your operating system. Extract the resulting zip file to a dedicated folder, then open a terminal in that folder.

Once you have a terminal open on your local machine, copy the line of code from your workstation into your local terminal and proceed with authorizing through the browser.


Copy the resulting string of characters from your local terminal into your remote terminal where it says
config_token.


Back in your workstation terminal, select
nwhen prompted to configure as a Shared Drive. Finally, selectqto quit the configuration steps.


Now that your workstation RClone is configured, the last thing you’ll need to do is mount your drive folder. Run the following script to mount the drive to the gdrive folder:
rclone mount gdrive: ~/gdrive --vfs-cache-mode writes --daemon --dir-cache-time 72h --vfs-cache-max-age 1h --log-level INFO

Now when you click in your gdrive folder in RStudio, you should see all of your files and folders from your NOAA Google Drive.

Access Google Drive files and folders in R using the googledrive package
The previous steps allow you to browse your files in a folder like you would any other folder. But if you need to access files directly through an R script without needing access to all of your files on your Google Drive, the R googledrive package is an easier way to load data files directly from your Google Drive into an R environment.
In an RStudio session, install the
googledrivepackage usinginstall.packages("googledrive"). This is a Tidyverse package that will also install if you runinstall.packages("tidyverse"). Note: remember to change your repository to Posit Package Manager, which uses precompiled binaries to speed things up.

Run the following script to configure the
googledrivepackage to work with your Google account. Replace “my.email@noaa.gov” with your NOAA email. Select 1 to cache your credentials so you don’t have to login in the future. This will open a login page where you will authorize your account.library(googledrive) library(gargle) drive_auth(token = credentials_user_oauth2( scopes = "https://www.googleapis.com/auth/drive", email = "my.email@noaa.gov"))

Copy the resulting character string to your clipboard, then paste it into your RStudio console.

To test that everything was set up correctly, run
drive_find(n=10)in your RStudio console. If everything was configured correctly, this should list the first 10 files/directories in your google drive.

