Skip to content

Instantly share code, notes, and snippets.

@ksonda
Last active May 25, 2022 14:47
Show Gist options
  • Save ksonda/f92db6a44c0f50555488084f49e5c8e6 to your computer and use it in GitHub Desktop.
Save ksonda/f92db6a44c0f50555488084f49e5c8e6 to your computer and use it in GitHub Desktop.
update-hydroshare-file
####################
# Using R to update a file in a Hydroshare resource
library(sf)
library(httr)
## make some data
d <- read_sf("https://reference.geoconnex.us/collections/states/items?limit=100")
## Write out the data
file <- "out.gpkg"
write_sf(d,file)
## Interact with Hydroshare
# API endpoint
api <- "https://www.hydroshare.org/hsapi/resource/"
# Set Hydroshare resource id and REST endpoints.
# Hydroshare does not have PUT or PATCH for files.
# So have to try DELETE and then POST
id <- "4bc8f1ee44644268a7b9edbd58b01047" #change to your resource
post_url <- paste0(api,id,"/files/./")
delete_url <- paste0(api,id,"/files/./",file)
try(
DELETE(delete_url,
authenticate("user", "pw")))
POST(post_url,
body = list(file=upload_file(file)),
encode = "multipart",
authenticate("user", "pw))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment