Skip to content

Commit

Permalink
Merge pull request #818 from DyfanJones/s3_download_file_redirect
Browse files Browse the repository at this point in the history
S3 download file redirect
  • Loading branch information
DyfanJones authored Aug 16, 2024
2 parents 0e694a7 + 18c756c commit dcfe898
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions paws.common/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* build endpoint with host_prefix (#804), thanks to @joseale2310 and @lyschoening for raising issue.
* fix `unix_time` ensure seconds is numeric (#804), thanks to @joseale2310 and @lyschoening for raising issue.
* fix stop anonymous credentials removing `x-amz-*` headers (#815) thanks to @cgostic for raising issue
* fix s3 redirect for download_file

# paws.common 0.7.4
* fix `transpose` to correctly parse lists with empty first elements (#791), thanks to @FMKerckhof for raising issue.
Expand Down
2 changes: 1 addition & 1 deletion paws.common/R/handlers_rest.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ rest_unmarshal_meta <- function(request) {

# Unmarshal the body from a REST protocol API response.
rest_unmarshal <- function(request) {
values <- request$data
values <- request[["data"]]
if ((payload_name <- tag_get(values, "payload")) != "") {
if ((payload_type <- tag_get(values[[payload_name]], "type")) == "blob") {
payload <- request$http_response$body
Expand Down
23 changes: 17 additions & 6 deletions paws.common/R/net.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ issue <- function(http_request) {
)

response <- HttpResponse(
status_code = httr::status_code(r),
header = httr::headers(r),
content_length = as.integer(httr::headers(r)$`content-length`),
status_code = r$status_code,
header = r$headers,
content_length = as.integer(r$headers$`content-length`),
# Prevent reading in data when output is set
body = (
if (is.null(http_request$dest)) httr::content(r, as = "raw") else raw()
)
body = resp_body(r, http_request$dest)
)

# Decode gzipped response bodies that are not automatically decompressed
Expand All @@ -153,6 +151,19 @@ issue <- function(http_request) {
return(response)
}

resp_body <- function(resp, path) {
if (is.null(path)) {
body <- httr::content(resp, as = "raw")
# return error message if call has failed or needs redirecting
} else if (resp$status_code %in% c(301, 400)) {
body <- readBin(path, "raw", file.info(path)$size)
unlink(path)
} else {
body <- raw()
}
return(body)
}

# Return whether an HTTP response body is (still) compressed by checking
# whether the body has a valid ZLIB header.
# See http://www.faqs.org/rfcs/rfc1950.html.
Expand Down

0 comments on commit dcfe898

Please sign in to comment.