使用 Ruby 将 CSV 负载上传到 Google 存储

Upload CSV payload to Google Storage using Ruby

我正在尝试使用 Ruby 直接将字符串负载上传到 Google 存储。但是,似乎没有直接的方法可以在不在磁盘中创建临时文件的情况下执行此操作。

我正在使用 CSV 库生成字符串负载。

当前方法建议将字​​符串有效负载存储在一个临时文件中,然后使用类似下面的代码将文件上传到 google 存储:

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name

  file = bucket.create_file local_file_path, file_name

有没有办法避免创建要上传的临时文件?

我找到了说明我们可以使用 any File-like object such as StringIO 直接上传字符串负载的文档。

这是我的代码:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-todo-app"
bucket.create_file StringIO.new("Hello world!"), "hello-world.txt"

例如转到此 linkCreating a File 部分

文档Link: https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage/Bucket.html#create_file-instance_method