diff --git a/R/ee_Initialize.R b/R/ee_Initialize.R index be25287f..6168971a 100644 --- a/R/ee_Initialize.R +++ b/R/ee_Initialize.R @@ -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 @@ -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)) { @@ -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( @@ -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")) @@ -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) diff --git a/R/ee_clean.R b/R/ee_clean.R index 6163dec0..ecc973ee 100755 --- a/R/ee_clean.R +++ b/R/ee_clean.R @@ -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)', diff --git a/R/ee_download.R b/R/ee_download.R index 7f990164..5ebfa595 100755 --- a/R/ee_download.R +++ b/R/ee_download.R @@ -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( diff --git a/R/ee_drive_to_local.R b/R/ee_drive_to_local.R new file mode 100644 index 00000000..24c00a40 --- /dev/null +++ b/R/ee_drive_to_local.R @@ -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 + } +} diff --git a/R/ee_image.R b/R/ee_image.R index 4c739910..57ae0093 100755 --- a/R/ee_image.R +++ b/R/ee_image.R @@ -77,6 +77,7 @@ #' Earth Engine Guide - Export data}. #' @return A stars-proxy object #' @family image download functions +#' @param drive_cred_path Path to a Google drive JSON token #' @export #' @examples #' \dontrun{ @@ -158,7 +159,9 @@ ee_as_stars <- function(image, add_metadata = TRUE, timePrefix = TRUE, quiet = FALSE, + drive_cred_path = NULL, ...) { + ee_check_packages("ee_as_stars", c("stars", "sf", "future")) # 1. From Earth Engine to the container (drive or gcs) @@ -173,8 +176,8 @@ ee_as_stars <- function(image, maxPixels = maxPixels, container = container, timePrefix = timePrefix, - quiet = quiet, - ... + quiet = quiet + #,... ) user_email <- ee_get_current_email() @@ -188,7 +191,10 @@ ee_as_stars <- function(image, via = via, metadata = add_metadata, public = public, - quiet = quiet + quiet = quiet, + drive_cred_path = drive_cred_path, + use_oob = use_oob + ) # Copy band names @@ -372,7 +378,11 @@ ee_as_raster <- function(image, add_metadata = TRUE, timePrefix = TRUE, quiet = FALSE, + drive_cred_path = NULL, + use_oob = NULL, + ...) { + ee_check_packages("ee_as_raster", c("raster")) ee_task <- ee_init_task( @@ -384,8 +394,8 @@ ee_as_raster <- function(image, container = container, maxPixels = maxPixels, timePrefix = timePrefix, - quiet = quiet, - ... + quiet = quiet + #,... ) user_email <- ee_get_current_email() @@ -399,7 +409,9 @@ ee_as_raster <- function(image, via = via, metadata = add_metadata, public = public, - quiet = quiet + quiet = quiet, + drive_cred_path = drive_cred_path, + use_oob = use_oob ) # Copy band names @@ -454,10 +466,14 @@ ee_init_task <- function(image, if (via == "drive") { ee_init_task_drive(image, region, dsn, scale, maxPixels, - timePrefix, container, quiet, ...) + timePrefix, container, quiet + #, ... + ) } else if (via == "gcs") { ee_init_task_gcs(image, region, dsn, scale, maxPixels, - timePrefix, container, quiet, ...) + timePrefix, container, quiet + #, ... + ) } else { stop("via argument invalid") } @@ -466,7 +482,9 @@ ee_init_task <- function(image, #' Create a Export task to GD #' @noRd ee_init_task_drive <- function(image, region, dsn, scale, maxPixels, timePrefix, - container, quiet, ...) { + container, quiet + , ... + ) { extras <- list(...) @@ -511,7 +529,7 @@ ee_init_task_drive <- function(image, region, dsn, scale, maxPixels, timePrefix, } # Are GD credentials loaded? - 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) # ee_Initialize(user = ee_user$email, drive = TRUE) @@ -537,8 +555,8 @@ ee_init_task_drive <- function(image, region, dsn, scale, maxPixels, timePrefix, fileFormat = "GEO_TIFF", region = region, maxPixels = maxPixels, - fileNamePrefix = file_name, - ... + fileNamePrefix = file_name + #,... ) # download parameter display @@ -658,10 +676,20 @@ ee_init_task_gcs <- function(image, region, dsn, scale, maxPixels, #' Passing an Earth Engine Image to Local #' @noRd -ee_image_local <- function(task, user_email, dsn, via, metadata, public, quiet) { +ee_image_local <- function(task, user_email, dsn, via, metadata, public, quiet, + drive_cred_path = NULL, use_oob = FALSE) { + if (via == "drive") { - ee_create_credentials_drive(user_email) - ee_image_local_drive(task, dsn, metadata, public, quiet) + ee_create_credentials_drive(email = user_email, + drive_cred_path = drive_cred_path, + use_oob = use_oob) + + ee_image_local_drive(task = task, + dsn, + metadata, + public, + quiet) + } else if (via == "gcs") { ee_create_credentials_gcs(user_email) ee_image_local_gcs(task, dsn, metadata, public, quiet) diff --git a/R/ee_imagecollection.R b/R/ee_imagecollection.R index c7534863..81dfbc9b 100755 --- a/R/ee_imagecollection.R +++ b/R/ee_imagecollection.R @@ -25,6 +25,7 @@ #' a prefix to export files. This parameter helps to avoid exported files #' with the same name. By default TRUE. #' @param quiet Logical. Suppress info message +#' @param drive_cred_path Path to Google Drive JSON token. #' @param ... Extra exporting argument. See \link{ee_image_to_drive} and #' @details #' \code{ee_imagecollection_to_local} supports the download of \code{ee$Images} @@ -131,6 +132,7 @@ ee_imagecollection_to_local <- function(ic, add_metadata = TRUE, timePrefix = TRUE, quiet = FALSE, + drive_cred_path = NULL, ...) { # check packages ee_check_packages("ee_imagecollection_to_local", "sf") @@ -204,6 +206,7 @@ ee_imagecollection_to_local <- function(ic, } ic_files <- list() + for (r_index in seq_len(ic_count)) { index <- r_index - 1 image <- ic %>% @@ -226,7 +229,8 @@ ee_imagecollection_to_local <- function(ic, public = public, add_metadata = add_metadata, timePrefix = timePrefix, - quiet = TRUE + quiet = TRUE, + drive_cred_path = drive_cred_path ) if (!lazy) { diff --git a/R/utils-auth.R b/R/utils-auth.R index b2075a59..ad06bcdb 100644 --- a/R/utils-auth.R +++ b/R/utils-auth.R @@ -193,22 +193,51 @@ ee_Dataset_creator <- function(eeDataset) { #' Create session info of the last init inside the #' folder ~/.config/earthengine/ -#' @noRd +#' @param email +#' @param user +#' @param drive_cre +#' @param drive_path +#' @param gcs_cre ee_sessioninfo <- function(email = NULL, user = NULL, drive_cre = NULL, - gcs_cre = NULL) { + drive_path = NULL, + gcs_cre = NULL, + drive_cred_path = NULL) { oauth_func_path <- system.file("python/ee_utils.py", package = "rgee") utils_py <- ee_source_python(oauth_func_path) sessioninfo <- sprintf( "%s/rgee_sessioninfo.txt", ee_utils_py_to_r(utils_py$ee_path()) ) + if (is.null(email)) { email <- NA } + + if (is.null(drive_path)) { + drive_path <- NA + } + + if (is.null(user)) { + user <- NA + } + + if (is.null(drive_cre)) { + drive_cre <- NA + } + + if (is.null(gcs_cre)) { + gcs_cre <- NA + } + df <- data.frame( - email = email, user = user, drive_cre = drive_cre, gcs_cre = gcs_cre, + email = email, + user = user, + drive_cre = drive_cre, + gcs_cre = gcs_cre, + drive_path = drive_path, + drive_cred_path = drive_cred_path, stringsAsFactors = FALSE ) write.table(df, sessioninfo, row.names = FALSE) diff --git a/R/utils-credentials.R b/R/utils-credentials.R index e28d9ed2..cb368431 100644 --- a/R/utils-credentials.R +++ b/R/utils-credentials.R @@ -1,6 +1,11 @@ #' Create credentials - Google Drive #' @noRd -ee_create_credentials_drive <- function(user=NULL, ee_utils, quiet) { +ee_create_credentials_drive <- function(user=NULL, + email = NULL, + ee_utils, + quiet, + drive_cred_path = NULL, + use_oob = FALSE) { # check googledrive R package installation ee_check_packages("ee_Initialize", "googledrive") @@ -18,28 +23,58 @@ ee_create_credentials_drive <- function(user=NULL, ee_utils, quiet) { ee_path_user <- sprintf("%s/%s", ee_path, user) } - # Load GD credentials (googledrive::drive_auth) - repeat { - full_credentials <- list.files(path = ee_path_user, full.names = TRUE) - drive_condition <- grepl(".*_.*@.*", basename(full_credentials)) - # If the googledrive credential does not exist run googledrive::drive_auth - if (!any(drive_condition)) { - suppressMessages( - googledrive::drive_auth( - email = NULL, - cache = ee_path_user - ) - ) - } else { - drive_credentials <- full_credentials[drive_condition] - email <- sub("^[^_]*_", "", basename(drive_credentials)) # Obtain the email + #if an auth token is supplied, use that first + + if(!is.null(drive_cred_path)){ + + token_info <- jsonlite::read_json(drive_cred_path) + + #grab email if not supplied + + if(is.null(email)){ + + email <- token_info$client_email + } + + # authorization + suppressMessages( googledrive::drive_auth( email = email, - cache = ee_path_user + path = drive_cred_path, + use_oob = use_oob) + ) - ) + + + }else{ + + + # Load GD credentials (googledrive::drive_auth) + repeat { + full_credentials <- list.files(path = ee_path_user, full.names = TRUE) + drive_condition <- grepl(".*_.*@.*", basename(full_credentials)) + + # If the googledrive credential does not exist run googledrive::drive_auth + if (!any(drive_condition)) { + suppressMessages( + googledrive::drive_auth( + email = NULL, + cache = ee_path_user + ) + ) + } else { + drive_credentials <- full_credentials[drive_condition] + email <- sub("^[^_]*_", "", basename(drive_credentials)) # Obtain the email + suppressMessages( + googledrive::drive_auth( + email = email, + cache = ee_path_user + ) + ) + + } # This lines is to avoid that users have multiple token file. # It delete the older one if the system detect two different token files. @@ -58,7 +93,7 @@ ee_create_credentials_drive <- function(user=NULL, ee_utils, quiet) { } # Move credential to the main folder is user is set - if (!is.null(user)) { + if (!is.null(user) & is.null(drive_cred_path)) { # Clean previous and copy new GD credentials in ./earthengine folder clean_drive <- list.files(ee_path, ".*_.*@.*", full.names = TRUE) %in% list.dirs(ee_path) unlink( @@ -70,7 +105,14 @@ ee_create_credentials_drive <- function(user=NULL, ee_utils, quiet) { overwrite = TRUE ) } - invisible(drive_credentials) + + + if(!exists("drive_credentials")){drive_credentials <- NA} + credentials_output <- data.frame(drive_credentials = drive_credentials, + drive_cred_path = drive_cred_path) + + invisible(credentials_output) + } @@ -84,7 +126,9 @@ ee_create_credentials_drive <- function(user=NULL, ee_utils, quiet) { #' (https://console.cloud.google.com/apis/credentials/serviceaccountkey/). #' Is necessary to save it (manually) inside the folder ~/.R/earthengine/USER/. #' @noRd -ee_create_credentials_gcs_ <- function(user, ee_utils) { +ee_create_credentials_gcs_ <- function(user, + ee_utils, + gcs_cred_path = NULL) { # check packages ee_check_packages("ee_Initialize", "googleCloudStorageR") @@ -101,36 +145,41 @@ ee_create_credentials_gcs_ <- function(user, ee_utils) { ee_path_user <- sprintf("%s/%s", ee_path, user) } - # gcs_credentials - full_credentials <- list.files(path = ee_path_user, full.names = TRUE) - gcs_condition <- grepl(".json", full_credentials) - - if (!any(gcs_condition)) { - gcs_text <- paste( - sprintf("Unable to find a service account key (SAK) file in: %s", crayon::bold(ee_path_user)), - "To solve this problem:", - " 1) download it from your Google cloud console", - " 2) validate it using rgee::ee_utils_sak_validate [OPTIONAL].", - " 3) Use rgee::ee_utils_sak_copy to set the SaK in rgee.", - "A tutorial to obtain the SAK file is available at:", - "> https://r-spatial.github.io/rgee/articles/rgee05.html", - crayon::bold("As long as you haven't saved a SKA file, the following functions will not work:"), - "- rgee::ee_gcs_to_local()", - "- ee_extract(..., via = \"gcs\")", - "- ee_as_raster(..., via = \"gcs\")", - "- ee_as_stars(..., via = \"gcs\")", - "- ee_as_sf(..., via = \"gcs\")", - "- sf_as_ee(..., via = \"gcs_to_asset\")", - "- gcs_to_ee_image", - "- raster_as_ee", - "- local_to_gcs", - "- stars_as_ee", - sep = "\n" - ) - gcs_info <- list(path = NA, message = gcs_text) - } else { - gcs_credentials <- full_credentials[gcs_condition] - googleCloudStorageR::gcs_auth(gcs_credentials) + if(is.null(gcs_cred_path)){ + + # gcs_credentials + full_credentials <- list.files(path = ee_path_user, full.names = TRUE) + gcs_condition <- grepl(".json", full_credentials) + + if (!any(gcs_condition)) { + gcs_text <- paste( + sprintf("Unable to find a service account key (SAK) file in: %s", crayon::bold(ee_path_user)), + "To solve this problem:", + " 1) download it from your Google cloud console", + " 2) validate it using rgee::ee_utils_sak_validate [OPTIONAL].", + " 3) Use rgee::ee_utils_sak_copy to set the SaK in rgee.", + "A tutorial to obtain the SAK file is available at:", + "> https://r-spatial.github.io/rgee/articles/rgee05.html", + crayon::bold("As long as you haven't saved a SKA file, the following functions will not work:"), + "- rgee::ee_gcs_to_local()", + "- ee_extract(..., via = \"gcs\")", + "- ee_as_raster(..., via = \"gcs\")", + "- ee_as_stars(..., via = \"gcs\")", + "- ee_as_sf(..., via = \"gcs\")", + "- sf_as_ee(..., via = \"gcs_to_asset\")", + "- gcs_to_ee_image", + "- raster_as_ee", + "- local_to_gcs", + "- stars_as_ee", + sep = "\n" + ) + gcs_info <- list(path = NA, message = gcs_text) + } else { + gcs_credentials <- full_credentials[gcs_condition] + googleCloudStorageR::gcs_auth(gcs_credentials) + + } + if (!is.null(user)) { unlink(list.files(ee_path, ".json", full.names = TRUE)) @@ -164,7 +213,8 @@ ee_create_credentials_gcs_ <- function(user, ee_utils) { #' where they can be automatically refreshed, as necessary. #' } #' @noRd -ee_create_credentials_earthengine <- function(user, auth_mode, auth_quiet, ee_utils, auth_params=NULL, ...) { +#' @param ee_cred_path A path to your earth engine credentials +ee_create_credentials_earthengine <- function(user, auth_mode, auth_quiet, ee_utils, auth_params=NULL, ee_cred_path = NULL, ...) { # Check sanity of earth-engine and return ee_utils.py module init <- ee_check_init() ee_utils <- init$ee_utils @@ -178,6 +228,18 @@ ee_create_credentials_earthengine <- function(user, auth_mode, auth_quiet, ee_ut ee_path_user <- sprintf("%s/%s", ee_path, user) } + #If path file is specified, copy it to where it needs to be + if(!is.null(ee_cred_path)){ + + path_condition <- file.copy( + from = ee_cred_path, + to = sprintf("%s/credentials", ee_path_user), + overwrite = TRUE + ) + + } + + # Obtain the path of the sub-folder credentials user_ee_credential <- sprintf("%s/credentials", ee_path_user) main_ee_credential <- sprintf("%s/credentials", ee_path)