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

Question: how do I restore the previous version of an object in a versioned bucket? #409

Open
wlandau opened this issue Nov 22, 2021 · 0 comments

Comments

@wlandau
Copy link

wlandau commented Nov 22, 2021

As part of ropensci/gittargets#6, I am trying to programmatically revert an object in an S3 bucket to an earlier version. From reading https://docs.aws.amazon.com/AmazonS3/latest/userguide/RestoringPreviousVersions.html, it looks like copying the object to itself (old version to current version) is the most efficient way to go. But I do not know how to use HTTP/REST headers, and I do not know how to select the old version of the object. Is there a header I can specify or a request I can submit to make this work in R with aws.s3?

library(aws.s3)
library(uuid)
bucket <- UUIDgenerate()
put_bucket(bucket)
#> [1] TRUE
put_versioning(bucket, "Enabled")
#> NULL
s3HTTP(
  verb = "GET",
  bucket = bucket,
  query = list(versioning = "")
)[1]
#> [1] "Enabled"

# old version
tmp <- tempfile()
writeLines("old", tmp)
out <- put_object(
  file = tmp,
  object = "object",
  bucket = bucket
)
version_old <- get_versions(bucket)$Version$VersionId

# new version
tmp <- tempfile()
writeLines("new", tmp)
out <- put_object(
  file = tmp,
  object = "object",
  bucket = bucket
)
version_new <- get_versions(bucket)$Version$VersionId

# Should be "new".
get_object("object", bucket, as = "text")
#> No encoding supplied: defaulting to UTF-8.
#> [1] "new\n"

# Should be "old". Why is it "new"?
get_object(
  "object",
  bucket,
  as = "text",
  headers = list(versionId = version_old)
)
#> No encoding supplied: defaulting to UTF-8.
#> [1] "new\n"

# Is this how I revert to the old version?
out <- copy_object(
  from_object = "object",
  to_object = "object",
  from_bucket = bucket,
  to_bucket = bucket,
  headers = list(versionId = version_old)
)
#> List of 4
#>  $ Code     : chr "InvalidRequest"
#>  $ Message  : chr "This copy request is illegal because it is trying to copy an object to itself without changing the object's met"| __truncated__
#>  $ RequestId: chr "JNVJ3H7B0B1KDFB3"
#>  $ HostId   : chr "Slte9SnTL0Xralznix6k93ez0Ja/8ij+pIqEMZmvq0zmfFPUdeFXWewWh80Ky/kViQ1ZhlVh44U="
#>  - attr(*, "headers")=List of 7
#>   ..$ x-amz-request-id : chr "JNVJ3H7B0B1KDFB3"
#>   ..$ x-amz-id-2       : chr "Slte9SnTL0Xralznix6k93ez0Ja/8ij+pIqEMZmvq0zmfFPUdeFXWewWh80Ky/kViQ1ZhlVh44U="
#>   ..$ content-type     : chr "application/xml"
#>   ..$ transfer-encoding: chr "chunked"
#>   ..$ date             : chr "Mon, 22 Nov 2021 19:13:59 GMT"
#>   ..$ server           : chr "AmazonS3"
#>   ..$ connection       : chr "close"
#>   ..- attr(*, "class")= chr [1:2] "insensitive" "list"
#>  - attr(*, "class")= chr "aws_error"
#>  - attr(*, "request_canonical")= chr "PUT\n/66553a84-0282-4ab8-87a5-e37a09aafcff/object\n\nhost:s3.amazonaws.com\nversionid:so6dk_b_t7CcEcbjj3Ok14sP4"| __truncated__
#>  - attr(*, "request_string_to_sign")= chr "AWS4-HMAC-SHA256\n20211122T191359Z\n20211122/us-east-1/s3/aws4_request\n8788609957ebcf0ab01338ed90ca4cd74e41747"| __truncated__
#>  - attr(*, "request_signature")= chr "AWS4-HMAC-SHA256 Credential=AKIA5IZZ3SMAULVNHKWK/20211122/us-east-1/s3/aws4_request,SignedHeaders=host;versioni"| __truncated__
#> NULL
#> Error in parse_aws_s3_response(r, Sig, verbose = verbose): Bad Request (HTTP 400).

Created on 2021-11-22 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info  🇸🇲  🗺️  👨🏿‍🌾   ──────────────────────────────────────────────────
#>  hash: flag: San Marino, world map, man farmer: dark skin tone
#> 
#>  setting  value
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2021-11-22
#>  pandoc   2.11.4 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package       * version date (UTC) lib source
#>  aws.s3        * 0.3.22  2021-11-03 [1] Github (cloudyr/aws.s3@621e0ad)
#>  aws.signature   0.6.0   2020-06-01 [2] CRAN (R 4.1.0)
#>  backports       1.3.0   2021-10-27 [2] CRAN (R 4.1.0)
#>  base64enc       0.1-3   2015-07-28 [2] CRAN (R 4.1.0)
#>  cli             3.1.0   2021-10-27 [1] CRAN (R 4.1.0)
#>  crayon          1.4.2   2021-10-29 [2] CRAN (R 4.1.0)
#>  curl            4.3.2   2021-06-23 [2] CRAN (R 4.1.0)
#>  digest          0.6.28  2021-09-23 [1] CRAN (R 4.1.0)
#>  ellipsis        0.3.2   2021-04-29 [2] CRAN (R 4.1.0)
#>  evaluate        0.14    2019-05-28 [2] CRAN (R 4.1.0)
#>  fansi           0.5.0   2021-05-25 [2] CRAN (R 4.1.0)
#>  fastmap         1.1.0   2021-01-25 [2] CRAN (R 4.1.0)
#>  fs              1.5.0   2020-07-31 [2] CRAN (R 4.1.0)
#>  glue            1.5.0   2021-11-07 [1] CRAN (R 4.1.0)
#>  highr           0.9     2021-04-16 [2] CRAN (R 4.1.0)
#>  htmltools       0.5.2   2021-08-25 [2] CRAN (R 4.1.0)
#>  httr            1.4.2   2020-07-20 [2] CRAN (R 4.1.0)
#>  knitr           1.36    2021-09-29 [1] CRAN (R 4.1.0)
#>  lifecycle       1.0.1   2021-09-24 [1] CRAN (R 4.1.0)
#>  magrittr        2.0.1   2020-11-17 [2] CRAN (R 4.1.0)
#>  mime            0.12    2021-09-28 [1] CRAN (R 4.1.0)
#>  pillar          1.6.4   2021-10-18 [1] CRAN (R 4.1.0)
#>  pkgconfig       2.0.3   2019-09-22 [2] CRAN (R 4.1.0)
#>  purrr           0.3.4   2020-04-17 [2] CRAN (R 4.1.0)
#>  R.cache         0.15.0  2021-04-30 [2] CRAN (R 4.1.0)
#>  R.methodsS3     1.8.1   2020-08-26 [2] CRAN (R 4.1.0)
#>  R.oo            1.24.0  2020-08-26 [2] CRAN (R 4.1.0)
#>  R.utils         2.11.0  2021-09-26 [2] CRAN (R 4.1.0)
#>  R6              2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
#>  reprex          2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang           0.4.12  2021-10-18 [1] CRAN (R 4.1.0)
#>  rmarkdown       2.11    2021-09-14 [1] CRAN (R 4.1.0)
#>  rstudioapi      0.13    2020-11-12 [2] CRAN (R 4.1.0)
#>  sessioninfo     1.2.1   2021-11-02 [2] CRAN (R 4.1.0)
#>  stringi         1.7.5   2021-10-04 [1] CRAN (R 4.1.0)
#>  stringr         1.4.0   2019-02-10 [2] CRAN (R 4.1.0)
#>  styler          1.6.2   2021-09-23 [2] CRAN (R 4.1.0)
#>  tibble          3.1.6   2021-11-07 [1] CRAN (R 4.1.0)
#>  utf8            1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  uuid          * 1.0-3   2021-11-01 [2] CRAN (R 4.1.0)
#>  vctrs           0.3.8   2021-04-29 [2] CRAN (R 4.1.0)
#>  withr           2.4.2   2021-04-18 [2] CRAN (R 4.1.0)
#>  xfun            0.28    2021-11-04 [1] CRAN (R 4.1.0)
#>  xml2            1.3.2   2020-04-23 [2] CRAN (R 4.1.0)
#>  yaml            2.2.1   2020-02-01 [2] CRAN (R 4.1.0)
#> 
#>  [1] /Users/c240390/Library/R/x86_64/4.1/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant