簡體   English   中英

Active Storage,指定一個 Google 存儲桶目錄 ?

[英]Active Storage, specify a Google bucket directory ?

我一直在 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 等。 我瀏覽了文檔,但還沒有找到指定更多目錄的方法,並且我所有的上傳都以存儲桶名稱結束。

抱歉,恐怕 Active Storage 不支持。 您打算使用它可以獨占使用的存儲桶配置 Active Storage。

也許你可以嘗試元編程,像這樣:

  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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM