Active Storage with Amazon S3 not saving with filename specified but using file key instead

梦想与她 提交于 2019-12-04 13:49:54

This is by design, from ActiveStorage. The file is stored by it's key and without extension on S3, but when the URL is generated by ActiveStorage, the disposition and filename are set.

def url(key, expires_in:, filename:, disposition:, content_type:)
  instrument :url, key: key do |payload|
    generated_url = object_for(key).presigned_url :get, expires_in: expires_in.to_i,
      response_content_disposition: content_disposition_with(type: disposition, filename: filename),
      response_content_type: content_type

    payload[:url] = generated_url

    generated_url
  end

end

This is probably done to avoid filename escaping issues that you'd run into otherwise.

You can read more about the Content-Disposition headers here.

You can still reference the name with filename accessor.

class User < ApplicationRecord
  has_one_attached :photo
  ...
end

filename = User.first.photo.filename
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!