将png图片上传到R中的GCS-可以单独上传,但不能上传整个目录

问题描述

我们有一个包含20000张图片的本地目录,需要转到GCS。使用R(和googleCloudStorageR),我们可以遍历每个图像,并按如下方式上传到GCS:

# setup
library(googleCloudStorageR)
gcs_auth(json_file = 'my/gcs/admin/creds.json')
all_png_images <- list.files('../path/to/local-images/directory')

# and loop
for(i in 1:length(all_png_images)) {
    googleCloudStorageR::gcs_upload(
      file = paste0(base_url,all_png_images[i]),bucket = 'my-bucket',name = paste0('images-folder/',predefinedAcl = 'default'
    )
}

,这很完美...但是,如果我可以直接指向目录并一次全部上传,而不是必须指向目录并遍历每个文件,那就更好了。我尝试使用gcs_save_all函数,但未成功:

googleCloudStorageR::gcs_save_all(
  directory = 'path-to-all-images',bucket = 'my-bucket'
)

引发错误2020-10-01 16:23:47 -- File size detected as 377.1 Kb 2020-10-01 16:23:47> Request Status Code: 400 Error: API returned: Cannot insert legacy ACL for an object when uniform bucket-level access is enabled. Read more at https://cloud.google.com/storage/docs/uniform-bucket-level-access

我试图找出为什么gcs_save_all无法正常工作,或者是否有其他方法可以在R中做到这一点。

解决方法

编辑:

opened an issue在googleCloudStorageR上使用,他们已经在GitHub的最新版本中解决了该问题,因此将其更新将允许您执行以下操作:

googleCloudStorageR::gcs_save_all(
  directory = 'path-to-all-images',bucket = 'my-bucket',predefinedAcl = "bucketLevel"
)

原始答案:

GCS有两种处理存储桶中对象的授权的方式:ACL和IAM。为了简化并让用户仅担心一种授权方案,存储桶可以启用“统一存储桶级访问”,从而阻止使用ACL。大概您已在存储桶上启用了此功能。

设置predefinedAcl = 'default'时,您指定GCS不应对ACL做任何特殊操作,这是在上传到具有统一存储桶级访问权限的存储桶时的正确设置。

似乎gcs_save_all没有这样的参数,默认值似乎是预定义的ACL“专用”,当启用统一存储桶级访问时,这不是有效选择。

,

需要更新库中的函数以支持存储桶级ACL,现在,您可以通过指定gcs_upload支持的“ bucketLevel” ACL将其功能复制到将要固定的功能。

例如

gcs_save_all <- function(directory = getwd(),bucket = gcs_get_global_bucket(),pattern = ""){

  tmp <- tempfile(fileext = ".zip")
  on.exit(unlink(tmp))

  bucket <- as.bucket_name(bucket)

  the_files <- list.files(path = directory,all.files = TRUE,recursive = TRUE,pattern = pattern)
  
  withCallingHandlers(
    zip::zip(tmp,files = the_files),deprecated = function(e) NULL)

  # modified to accept ACL on bucket
  gcs_upload(tmp,bucket = bucket,name = directory,predefinedAcl = "bucketLevel")

}