ruby - 使用 Ruby 将 CSV 负载上传到 Google 存储
问题描述
我正在尝试使用 Ruby 直接将字符串有效负载上传到 Google Storage。但是,如果不在磁盘中创建临时文件,似乎没有直接的方法可以做到这一点。
我正在使用 CSV 库来生成字符串有效负载。
当前方法建议将字符串有效负载存储在临时文件中,然后使用如下代码将文件上传到谷歌存储:
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"
例如,转到Creating a File
此链接的部分
推荐阅读
- flutter - 从底部颤动的页面弹出
- vue.js - Vue.js如何避免不同功能中的重复代码?
- javascript - TypeError:Mobile Safari 中 pubads_impl_XXX.js 中的非法调用
- ruby-on-rails - Ruby on Rails - 在某些字段验证失败后丢失表单值
- sapper - 预加载不会在导入到 _layout 的组件上运行
- excel - 在公式行中查找/搜索
- javascript - Google 表格数据应用程序脚本未在 WebApps 中显示值
- git - Heroku - 如何更新现有的 Heroku 应用程序?
- python - pyqt5 的颜色下拉选择器
- drupal-8 - drupal 8 不能在我的函数中使用其他类