Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft method for non-interactive authentication #336

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions R/ee_Initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#' \item{4. }{appdefault - read from existing $GOOGLE_APPLICATION_CREDENTIALS file}
#' \item{5. }{None - a default mode is chosen based on your environment.}
#' }
#' @param email User email (Optional)
#' @param use_oob Should out of band authentication be used for Google Drive?
#' Default is FALSE.
#' @param drive_cred_path Optional path to google drive credentials JSON key (Default is NULL)
#' @param gcs_cred_path Optional path to google cloud storage credentials JSON key (Default is NULL)
#' @param ee_cred_path Optional path to earth engine JSON key (Default is NULL)
#' @param ... Extra exporting argument. See \link{ee_Authenticate}.
#'
#' @importFrom utils read.table browseURL write.table packageVersion
Expand Down Expand Up @@ -74,20 +80,33 @@ ee_Initialize <- function(user = NULL,
quiet = FALSE,
auth_mode = "notebook",
auth_quiet = FALSE,
email = NULL,
drive_path = NULL,
use_oob = FALSE,
drive_cred_path = NULL,
gcs_cred_path = NULL,
ee_cred_path = NULL,
...
) {

# Set google-cloud-sdk in your PATH systen
gcloud_path <- Sys.getenv("EARTHENGINE_GCLOUD", unset = NA)
if (!is.na(gcloud_path))

#If provided, set GCS path
if(!is.null(gcs_cred_path)){
Sys.setenv("GCS_AUTH_FILE" = gcs_cred_path)
}

if (!is.na(gcloud_path)){
Sys.setenv(PATH = sprintf("%s:%s", Sys.getenv("PATH"), gcloud_path))
}

# Check sanity of earth-engine and return ee_utils.py module
init <- ee_check_init()
ee_utils <- init$ee_utils

# rgee init message
if (!quiet) ee_message_01(user, init$earthengine_version)
if (!quiet){ ee_message_01(user, init$earthengine_version)}

# If user is not NULL create, then save the credentials in a subfolder.
if (!is.null(user)) {
Expand All @@ -106,33 +125,43 @@ ee_Initialize <- function(user = NULL,
ee_check_packages("ee_Initialize", "googledrive")

# drive init message
if (!quiet) ee_message_02(init = TRUE)
if (!quiet){ ee_message_02(init = TRUE)}

# If the user is not NULL copy the drive credential in the subfolder
drive_credentials <- ee_create_credentials_drive(user, ee_utils, quiet = quiet)
drive_credentials <- ee_create_credentials_drive(user,
ee_utils,
quiet = quiet,
drive_cred_path = drive_cred_path,
use_oob = use_oob)
test_drive_privileges(user)

if (!quiet) ee_message_02(init = FALSE)
if (!quiet){ ee_message_02(init = FALSE)}
}

if (gcs) {
ee_check_packages("ee_Initialize", "googleCloudStorageR")

if (!quiet) ee_message_03(init=TRUE, gcs_credentials)
if (!quiet){ ee_message_03(init=TRUE, gcs_credentials)}

# Load GCS credentials
gcs_credentials <- ee_create_credentials_gcs(user, ee_utils)

if (!quiet) ee_message_03(init=FALSE, gcs_credentials)
if (!quiet){ ee_message_03(init=FALSE, gcs_credentials)}
}

## rgee session file
options(rgee.gcs.auth = gcs_credentials[["path"]])

if (!quiet) ee_message_04(init = TRUE)
if (!quiet){ ee_message_04(init = TRUE)}

# If user is not NULL copy the credentials from sub to main folder
ee_create_credentials_earthengine(user, auth_mode, auth_quiet, ee_utils, ...)
ee_create_credentials_earthengine(user,
auth_mode,
auth_quiet,
ee_utils,
ee_cred_path = ee_cred_path
#, ...
)

tryCatch(expr = {
ee$Initialize(
Expand All @@ -158,20 +187,23 @@ ee_Initialize <- function(user = NULL,
}
})

if (!quiet) ee_message_04(init = FALSE)
if (!quiet){ ee_message_04(init = FALSE)}

# check if the GEE acount has been created a GEE mainfolder
ee_user <- ee_check_root_folder()

ee_user <- ee_check_root_folder()

options(rgee.ee_user = ee_user)

ee_sessioninfo(
email = if (is.null(user)) "ndef" else user,
email = if(!is.null(email)){email}else{if (is.null(user)){ "ndef" }else{ user}},
user = ee_user,
drive_cre = drive_credentials,
gcs_cre = gcs_credentials[["path"]]
drive_cre = drive_credentials$drive_credentials,
gcs_cre = gcs_credentials[["path"]],
drive_cred_path = drive_credentials$drive_cred_path
)

if (!quiet) ee_message_06(gcs_credentials, ee_user)
if (!quiet){ ee_message_06(gcs_credentials, ee_user)}

# Add Dataset attribute
eeDataset <- jsonlite::read_json(system.file("dataset.json", package="rgee"))
Expand Down Expand Up @@ -313,6 +345,7 @@ ee_Authenticate <- function(
# gcs init message
if (verbose) ee_message_03(init = TRUE, gcs_credentials)


# Create the gcs credential
gcs_credentials <- ee_create_credentials_gcs(user=user, ee_utils)

Expand Down
2 changes: 1 addition & 1 deletion R/ee_clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ee_clean_container <- function(name = "rgee_backup",

if (type == "drive") {
ee_check_packages("ee_download_drive", "googledrive")
if (is.na(ee_user[["drive_cre"]])) {
if (is.na(ee_user[["drive_cre"]]) & is.na(ee_user[["drive_cred_path"]])) {
stop(
"Google Drive credentials were not loaded.",
' Run ee_Initialize(user = "myemail", drive = TRUE)',
Expand Down
4 changes: 3 additions & 1 deletion R/ee_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,9 @@ ee_drive_to_local <- function(task,

# Check credentials
ee_user <- ee_exist_credentials()
if (is.na(ee_user[["drive_cre"]])) {


if (is.na(ee_user[["drive_cre"]]) & is.na(ee_user[["drive_cred_path"]])) {
drive_credential <- ee_create_credentials_drive(ee_user$email)
ee_save_credential(pdrive = drive_credential)
message(
Expand Down
162 changes: 162 additions & 0 deletions R/ee_drive_to_local.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#' @noRd
ee_drive_to_local <- function(task,
dsn,
overwrite = TRUE,
consider = TRUE,
public = FALSE,
metadata = FALSE,
quiet = FALSE) {

# Check packages
ee_check_packages("ee_drive_to_local", "googledrive")

# Check credentials

ee_user <- ee_exist_credentials()

if (is.na(ee_user[["drive_cre"]])& is.na(ee_user[["drive_cred_path"]])) {
drive_credential <- ee_create_credentials_drive(ee_user$email)
ee_save_credential(pdrive = drive_credential)
message(
"Google Drive credentials were not loaded.",
" Running ee_Initialize(user = '", ee_user[["email"]], "', drive = TRUE)",
" to fix."
)
}

# global parameter of a task
gd_folder <- basename(ee$batch$Task$status(task)[["destination_uris"]])
gd_ExportOptions <- task[["config"]][["fileExportOptions"]]
gd_filename <- gd_ExportOptions[["driveDestination"]][["filenamePrefix"]]

# Select a google drive file considering the filename and folder
count <- 1
files_gd <- try(googledrive::drive_find(
q = sprintf("'%s' in parents", gd_folder),
q = sprintf("name contains '%s'", gd_filename)
), silent = TRUE)

while (any(class(files_gd) %in% "try-error") & count < 5) {
files_gd <- try(googledrive::drive_find(
q = sprintf("'%s' in parents", gd_folder),
q = sprintf("name contains '%s'", gd_filename)
), silent = TRUE)
count <- count + 1
}

if (public) {
googledrive::with_drive_quiet({
files_gd <- googledrive::drive_share_anyone(
file = files_gd
)
})
}

# (Problem) Google Drive support files with the same name
if (nrow(files_gd) > 0) {
ee_getTime <- function(x) {
gd_file_date <- files_gd[["drive_resource"]][[x]][["createdTime"]]
as.POSIXct(gd_file_date)
}
createdTime <- vapply(seq_len(nrow(files_gd)), ee_getTime, 0)
files_gd <- files_gd[order(createdTime, decreasing = TRUE), ]
if (isTRUE(consider)) {
choices <- c(files_gd[["name"]],'last','all')
if (nrow(files_gd) == 1) {
file_selected <- 1
} else {
file_selected <- menu(
choices = choices,
title = paste0(
"Multiple files with the same name",
" (sorted according to the created time argument):"
)
)
}
if (choices[file_selected] == 'last') {
files_gd <- files_gd[1,]
} else if (choices[file_selected] == 'all') {
files_gd <- files_gd
} else {
files_gd <- files_gd[file_selected, ]
}
} else if (consider == "last") {
files_gd <- files_gd[1, ]
} else if (consider == "all") {
files_gd <- files_gd
} else {
stop("consider argument was not defined properly.")
}
} else {
stop(
"File does not exist in Google Drive.",
" Please verify if the task finished properly."
)
}

# Choose the right file using the driver_resource["originalFilename"]
fileformat <- toupper(gd_ExportOptions[["fileFormat"]])

if (missing(dsn)) {
ee_tempdir <- tempdir()
filenames_local <- sprintf("%s/%s", ee_tempdir, basename(files_gd$name))
} else {
pattern <- "(.*)(\\..*)$"
element_len <- length(files_gd$name)
# Neccesary for large GEOTIFF and TFRecord files
if (task$task_type == "EXPORT_IMAGE" & element_len > 1) {
file_ft <- sprintf(
"-%04d%s",
seq_len(element_len),
sub(pattern, "\\2", files_gd$name)
)
} else {
file_ft <- sub(pattern, "\\2", files_gd$name)
}
dsn_n <- sub(pattern,"\\1",basename(dsn))
filenames_local <- sprintf("%s/%s%s",dirname(dsn), dsn_n, file_ft)
}
# it is necessary for ESRI shapefiles
filenames_local <- ee_sort_localfiles(filenames_local, fileformat)
to_download <- sort_drive_files(files_gd, fileformat)

# if (nrow(to_download) > 4) {
# stop(
# "Impossible to download multiple geometries as SHP.",
# " Try to define the fileFormat argument as GEO_JSON"
# )
# }
for (index in seq_len(nrow(to_download))) {
if (!quiet) {
googledrive::with_drive_quiet({
googledrive::drive_download(
file = to_download[index, ],
path = filenames_local[index],
overwrite = overwrite
)
})
} else {
googledrive::drive_download(
file = to_download[index, ],
path = filenames_local[index],
overwrite = overwrite
)
}
}

if (metadata) {
list(
dsn = filenames_local,
metadata = list(
ee_id = task$id,
drive_name = to_download$name,
drive_id = to_download$id,
drive_download_link = sprintf(
"https://drive.google.com/uc?id=%s&export=download",
to_download$id)
)
)
} else {
filenames_local
}
}
Loading