我在使用 Active Storage 时遇到问题。当我上传到 Amazon S3 时,不是使用原始名称将文件保存在存储桶内,例如myfile.zip
它正在将其另存为key
与该文件关联。所以在 Cyberduck 中我看到这样的东西:5YE1aJQuFYyWNr6BSHxhQ48t
。没有任何文件扩展名。
我不确定 Rails 5 中是否有某些设置,或者是否在 Amazon S3 中,但我花了几个小时在谷歌上搜索以找出发生这种情况的原因。
任何指点将不胜感激!
此致,
安德鲁
这是 ActiveStorage 设计的。该文件通过其密钥存储在 S3 上且不带扩展名,但是当 ActiveStorage 生成 URL 时,配置和文件名已设置 https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/s3_service.rb#L70.
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
这样做可能是为了避免您在其他情况下遇到的文件名转义问题。
您可以阅读更多有关Content-Disposition
标头here https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)