Skip to content

Commit

Permalink
paging for gcs_list_objects (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Feb 19, 2017
1 parent 566e58c commit fd2adad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add support for `gs://` style URLs for object names (#57 - thanks seandavi)
* Add what the public URL would be to an objects print method (#59 - thanks mwhitaker)
* Add check to `gcs_get_bucket()` to only expect length 1 character vectors for bucket name. (#60)
* Add paging for `gcs_object_list` (#58 - thanks @G3rtjan)

# 0.2.0

Expand Down
24 changes: 24 additions & 0 deletions R/objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ gcs_list_objects <- function(bucket = gcs_get_global_bucket(),
testthat::expect_length(bucket, 1)

parse_lo <- function(x){
nextPageToken <- x$nextPageToken
x <- x$items
x$timeCreated <- timestamp_to_r(x$timeCreated)
x$updated <- timestamp_to_r(x$updated)
x$kind <- NULL
x$size <- vapply(as.numeric(x$size), function(x) format_object_size(x, "auto"), character(1))

attr(x, "nextPageToken") <- nextPageToken
x
}

Expand All @@ -41,6 +44,27 @@ gcs_list_objects <- function(bucket = gcs_get_global_bucket(),
data_parse_function = parse_lo)
req <- lo()

## page through list if necessary
if(!is.null(attr(req, "nextPageToken"))){
npt <- attr(req, "nextPageToken")

lo2 <- googleAuthR::gar_api_generator("https://www.googleapis.com/storage/v1/",
path_args = list(b = bucket,
o = ""),
pars_args = list(pageToken = npt),
data_parse_function = parse_lo)


while(!is.null(npt)){
myMessage("Paging through results: ", npt, level = 3)
more_req <- lo2(pars_arguments = npt)
npt <- attr(more_req, "nextPageToken")
req <- rbind(req, more_req)
}
}



out_names <- switch(detail,
summary = c("name", "size", "updated"),
more = c("name", "size", "bucket", "contentType", "timeCreated", "updated", "storageClass"),
Expand Down

0 comments on commit fd2adad

Please sign in to comment.