不要将文件嵌套在 rails 个活动存储中

Do not nest files in rails active storage

似乎默认情况下,Rails Active Storage 通过关联的 active_storage_blob key.

嵌套您的文件上传

默认行为的规则如下所示。在 <Rails.root>/storage/ 目录中:

例如:特定文件的关联 active_storage_blobkey 是:2HadGpe3G4r5ygdgdfh5534346,它看起来像下面这样:

我不想要这种嵌套行为。我想将文件平放在存储目录中。所以我只是希望它看起来像这样:

.

我该怎么做? google 搜索和通读 Active Storage Rails Guides 没有找到解决方案。

也只是出于好奇:为什么这是默认行为?

the code of the ActiveStorage DiskService, I found the code which generates the folder structure. All is conveniently contained within a single function 中挖掘:

def folder_for(key)
  [ key[0..1], key[2..3] ].join("/")
end

这样可以很容易地通过一个简单的补丁消除两个字母的子文件夹结构:

module ActiveStorage
  class Service::DiskService < Service
    private
      def folder_for(key)
        ""
      end
  end
end

最好对此补丁进行一些测试,但据我所知它应该可以正常工作。

第二个问题的答案我无法通过查看 DiskService 代码来确定。这个文件夹结构没有任何线索,所以原因可能在别处。它可能完全出于装饰目的而完成,以避免在大型服务器上出现巨大的单个文件夹 blob。或许知道的人可以评论一下。