del cueto
del cueto

Reputation: 111

Active Storage, specify a Google bucket directory ?

I've been implementing an Active Storage Google strategy on Rails 5.2, at the moment I am able to upload files using the rails console without problems, the only thing I am missing is if there is a way to specify a directory inside a bucket. Right now I am uploading as follows

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

The configuration on my storage.yml

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

But in my bucket there are different directories such as bucketname/department1 and such. I been through the documentation and have not found a way to specify further directories and all my uploads end up in bucket name.

Upvotes: 1

Views: 1711

Answers (2)

bibs
bibs

Reputation: 99

Maybe you can try metaprogramming, something like this:

  1. Create config/initializers/active_storage_service.rb to add set_bucket method to 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. Update your bucket before uploading or downloading files
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")

Upvotes: 1

George Claghorn
George Claghorn

Reputation: 26535

Sorry, I’m afraid Active Storage doesn’t support that. You’re intended to configure Active Storage with a bucket it can use exclusively.

Upvotes: 2

Related Questions