简体   繁体   English

从 RStudio 将多个文件上传到 Google Cloud

[英]Upload multiple files to Google Cloud from RStudio

I am generating multiple html files using a combination of R and R markdown.我正在使用 R 和 R Z590FC197FE7320AA2EC03687A 的组合生成多个 html 文件。 Uploading multiple and individually identified files to AWS s3 is easy using a for loop and a put object command.使用 for 循环和 put object 命令可以轻松将多个单独标识的文件上传到 AWS s3。 Here's an example:这是一个例子:

for (i in 1:nrow(Data)){
  rmarkdown::render(input = "report.Rmd",
                    output_format = "html_document",
                    output_file = paste("output/report_", i, ".html", sep =''))}
library(aws.s3)
Sys.setenv(AWS_ACCESS_KEY_ID = "XXXX",
           AWS_SECRET_ACCESS_KEY = "YYYYY",
           AWS_DEFAULT_REGION = "ZZZZ")

# put_object to upload to AWS
for (i in 1:nrow(Data)){
  put_object(file = paste("/Users/me/output/report_", i, ".html", sep = ''),
             object = paste("report_", i, ".html", sep = ''), bucket = "mybucket")}

I am trying to understand how to do the same with Google Cloud Storage.我试图了解如何对 Google Cloud Storage 做同样的事情。

library(googleCloudStorageR)
library(googleAuthR)


Sys.setenv("GCS_DEFAULT_BUCKET" = "reports",
           "GCS_AUTH_FILE" = "file/path/to.json")

I can upload a single output without issues:我可以毫无问题地上传单个 output:

gcs_upload(file = "output/report_1.html", bucket = "reports")

When I try to upload multiple, individually-named files using a loop it fails.当我尝试使用循环上传多个单独命名的文件时,它会失败。

for (i in 1:nrow(Data)){
  gcs_upload(file = paste("output/report_", i, ".html", sep = ""), bucket = "reports")
}

This code will write the first html output, occasionally the second and never the complete set.此代码将编写第一个 html output,偶尔会编写第二个,但永远不会完整。

All help appreciated.所有帮助表示赞赏。

UPDATE: I have abandoned this effort.更新:我已经放弃了这项工作。 A workaround is to use a for loop to upload to AMZN and schedule a repeating transfer to GCS using GCS commands.一种解决方法是使用 for 循环上传到 AMZN,并使用 GCS 命令安排重复传输到 GCS。 Other workarounds use CLI but I don't want to go that way.其他解决方法使用 CLI,但我不想 go 那样。 I suspect the lack of response to this thread indicates there is little demand to create an upload from R to GCS.我怀疑对这个线程没有响应表明几乎不需要创建从 R 到 GCS 的上传。 Separately, shout out to whomever wrote the R aws s3 package.另外,请向编写 R aws s3 package 的人大喊大叫。 Works beautifully!工作精美!

I think it was probably due to not using a name argument in the loop, which by default derived from the name of the R object so when looping caused an error.我认为这可能是由于没有在循环中使用name参数,默认情况下,该名称源自 R object 的名称,因此在循环时会导致错误。

This would have probably worked:这可能会奏效:

for (i in 1:nrow(Data)){
  the_file_name <- paste("output/report_", i, ".html", sep = "")
  gcs_upload(the_file_name, name = the_file_name, bucket = "reports")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM