0

我一直在 Rails 5.2 上实施 Active Storage Google 策略,目前我可以毫无问题地使用 rails 控制台上传文件,我唯一缺少的是是否有一种方法可以指定存储桶内的目录。现在我上传如下

bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")

我的 storage.yml 上的配置

google:
  service: GCS
  project: my-project 
  credentials: <%= Rails.root.join("config/myfile.json") %>
  bucket: bucketname

但是在我的存储桶中有不同的目录,例如 bucketname/department1 等。我浏览了文档,但没有找到指定更多目录的方法,我所有的上传都以存储桶名称结尾。

4

2 回答 2

2

抱歉,恐怕 Active Storage 不支持。您打算为 Active Storage 配置一个可以独占使用的存储桶。

于 2018-05-29T21:51:24.753 回答
1

也许你可以尝试元编程,像这样:

  1. 创建 config/initializers/active_storage_service.rb 将 set_bucket 方法添加到 ActiveStorage::Service
module Methods
  def set_bucket(bucket_name)
    # update config bucket
    config[:bucket] = bucket_name

    # update current bucket
    @bucket = client.bucket(bucket_name, skip_lookup: true)
  end
end
ActiveStorage::Service.class_eval { include Methods }
  1. 在上传或下载文件之前更新您的存储桶
ActiveStorage::Blob.service.set_bucket "my_bucket_name"

bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")

于 2020-11-24T04:24:07.950 回答