Interest in supporting multiple containers in ActiveStorage Azure service?

Hi! I’m new to the community so I hope I’m going about this the right way.

I’d like to use different containers on Azure blob store for each attachment type that I have in ActiveStorage. I’m wondering what the community’s opinion is on this feature, and if it’s something I should invest some time in implementing, or create an issue for on Github.

For example, if you have a User model with an attached avatar and a Post model with an attached image, it would be nice to be able to store avatar images in a container called “user-avatars” and image images in a container called “post-images”.

This could be implemented either as a configuration option in the model, like: has_one_attached :avatar, container: "user-avatars

Or in storage.yml, like:

azure:
  service: ...
  storage_account_name: ...
  ...
  containers:
    user:
      avatar: user-avatars
    post:
      image: post-images

Honestly the former seems way preferable to me, but just trying to convey an idea of what the feature is.

This is useful to me because I’m working on a large Rails codebase with many terabytes of file attachments. I’m trying to do a low-impact migration from Paperclip, in which I initially don’t need to move all the attachments on Azure, but rather override Active Storage’s path logic to point to the proper container. In the process, I’m already monkey-patching some code to allow ActiveStorage to use the same paths Paperclip used. However, the issue of being able to support multiple containers is distinct enough from this effort that I thought it could be its own feature.

Hello Katy, welcome to the community.

What you want to do should already be possible by using the service: argument on the has_one_attached method: ActiveStorage::Attached::Model Basically, you can declare your different containers as different services and use a different service for each model.

Does this not work for you?

Hi Ufuk,

Good call, thanks! We’re on Rails 6 so I didn’t see that option. Good reminder to always look at the latest when evaluating a solution :slight_smile:

So, your proposed solution sounds like it would absolutely work. It is a BIT awkward, since the use case here is just that we need different containers within the same Azure storage account, but what we’re doing to achieve that is creating (number-of-distinct-containers) different instances of the service, which all differ only in one instance attribute. So it seems like it would work, but doesn’t seem to be created for this use case.

Regardless, I’m happy to use this solution. Thanks!

Indeed, it is a different kind of abstraction. However, since the configuration of services in done in YAML, you can use anchors, references and merges to declare the account information in one service only and to only change the container name in the other service definitions. Reusing data with YAML Anchors, Aliases and Merge Keys | tinita [blogs.perl.org]

Yes! That will definitely be the right way to represent the YAML. Thank you for all the recommendations.

1 Like