使用Amazon S3的Active Storage不会使用指定的文件名保存,而是使用文件密钥保存 [英] Active Storage with Amazon S3 not saving with filename specified but using file key instead

查看:97
本文介绍了使用Amazon S3的Active Storage不会使用指定的文件名保存,而是使用文件密钥保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Active Storage时遇到问题.当我上载到Amazon S3时,与其将文件保存为原始名称(如myfile.zip),不如将其保存在与该文件关联的key中.因此,在Cyber​​duck中,我看到的是这样的内容:5YE1aJQuFYyWNr6BSHxhQ48t.没有任何文件扩展名.

I am having an issue with Active Storage. When I upload to Amazon S3, instead of saving the file inside the bucket with the original name like myfile.zip it is saving it as the key which is associated with that file. So in Cyberduck I am seeing something like this: 5YE1aJQuFYyWNr6BSHxhQ48t. Without any file extension.

我不确定Rails 5中是否有某些设置,或者它是否在Amazon S3中,但是我花了几个小时在谷歌上搜索以找出发生这种情况的原因.

I am not sure if there is some setting in Rails 5 or whether it is within Amazon S3 but I have spent hours Googling around to figure out why this is happening.

任何指针将不胜感激!

最诚挚的问候, 安德鲁

Best regards, Andrew

推荐答案

这是ActiveStorage设计的.该文件由其密钥存储,在S3上没有扩展名,但是当URL由ActiveStorage生成时,

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

结束

这样做可能是为了避免否则会遇到文件名转义的问题.

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

您可以阅读有关Content-Disposition标头的更多信息这里.

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

这篇关于使用Amazon S3的Active Storage不会使用指定的文件名保存,而是使用文件密钥保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆