Ruby on Rails AWS S3 下载 URL

fra*_*ama 4 ruby-on-rails amazon-s3

如何为用户形成 url 链接,以便当用户单击该链接时,它会强制他们下载 AWS S3 对象?

我见过这两种解决方案:使用 send_file 从 Amazon S3 下载文件?使用 send_file 从 Amazon S3 下载文件?但是,它们似乎引用了旧的 AWS S3 v1 SDK,并且 v2 AWS S3 SDK 中似乎没有 url_for。

谢谢。

fra*_*ama 8

最终使用以下代码片段来解决。希望这对其他人有帮助。

presigner = Aws::S3::Presigner.new
    url = presigner.presigned_url(:get_object, #method
                    bucket: ENV['S3_BUCKET'], #name of the bucket
                    key: s3_key, #key name
                    expires_in: 7.days.to_i, #time should be in seconds
                    response_content_disposition: "attachment; filename=\"#{filename}\""
                    ).to_s
Run Code Online (Sandbox Code Playgroud)