在我的Rails 4应用程序中,我使用PaperClip将我的图像存储在S3上 . 我在S3存储桶中的任何图像都是这样的:
现在为了通过 AWS CloudFront CDN 提供图像,我遵循this article建议在我的Image模型中设置 :path
属性 . 它的确切 Value 在这里?
has_attached_file :file, styles: { small: '120x50', medium: '350x350' }, :path => "images/files/../../:id/:style/:filename"
具体来说,我不确定如何处理上面的图像URL中的 /000/001/ 部分 . 因为这些是PaperClip在S3上保存图像时创建的任意文件夹 . 否则, :id, :style and :filename
正在运行时正确替换它们的值 .
3 回答
使用文件系统时 there are limitations as to the amount of files that can be stored in each folder .
在Linux系统上,如果要在文件夹中存储超过32,000个文件,则会收到错误 . For this reason paperclip automatically partitions your files into separate folders ,这就是上面例子中000/001/920所做的......这是必要的 .
它取对象id为1920,并在其前面加上0来获得9位数,然后将它们分成不同的分区 . 如果您必须手动设置路径,而不是让paperclip处理此逻辑,您还必须手动对文件进行分区,否则最终会遇到大问题 .
我认为你不需要做任何关于:path,你需要确保config.action_controller.asset_host指向你的CloudFront域名,而不是S3
您正在寻找的符号是:id_partition .
所以整条路都是
images/files/:id_partition/:style/:filename