[英]how to link a single private S3 file in rails for download
I want to provide a link for users to download a non-public file from Amazon S3 in my rails app. 我想为用户提供一个链接,以便在我的rails应用程序中从Amazon S3下载非公共文件。 This is not a user-uploaded file, just a single file that I've uploaded that anyone who's logged in should be able to download.
这不是用户上传的文件,只是我上传的一个文件,任何登录的人都可以下载。
I'm getting lost in S3 documentation/parameters/syntax. 我在S3文档/参数/语法中迷路了。 Like hours lost.
就像失去了几个小时。 Here's the Amazon url to the non-public file: https://s3-us-west-1.amazonaws.com/physicianneedtool/static/Historical_Payer_Profile.xlsx .
这是非公开文件的Amazon网址: https : //s3-us-west-1.amazonaws.com/physicianneedtool/static/Historical_Payer_Profile.xlsx 。 The S3_BUCKET, S3_KEY, S3_SECRET_KEY and S3_REGION variables already work with my carrierwave setup in the app, so I know they're good.
S3_BUCKET,S3_KEY,S3_SECRET_KEY和S3_REGION变量已经可以在应用程序中与我的载波设置一起使用,所以我知道它们很好。
My current non-working code: 我当前的无效代码:
s3 = Aws::S3::Resource.new(region: ENV["S3_REGION"],
access_key_id: ENV["S3_KEY"],
secret_access_key: ENV["S3_SECRET_KEY"])
@file_url = s3.bucket(ENV["S3_BUCKET"])
.object('Historical_Payer_Profile.xlsx')
.get(response_target: 'static/Historical_Payer_Profile.xlsx')
The error I get is Aws::S3::Errors::NoSuchKey. 我得到的错误是Aws :: S3 :: Errors :: NoSuchKey。 In rails console, I found that s3.bucket(ENV["S3_BUCKET"]).object('Historical_Payer_Profile.xlsx') returns a seemingly valid S3 object, so it looks like the error is caused by the .get call at the end.
在Rails控制台中,我发现s3.bucket(ENV [“ S3_BUCKET”])。object('Historical_Payer_Profile.xlsx')返回一个看似有效的S3对象,因此看起来错误是由结尾的.get调用引起的。 What am I doing wrong there?
我在那里做错了什么? The path specified for response_target is the exact path I get from the copy path button in the S3 console.
为response_target指定的路径是我从S3控制台中的“复制路径”按钮获得的确切路径。
You can use Aws::S3::Presigner , it allows you to create presigned URLs to share/download private files, and you can make this url expire after some time. 您可以使用Aws :: S3 :: Presigner ,它允许您创建预签名的URL来共享/下载私有文件,并且可以使此URL在一段时间后过期。
presigner = Aws::S3::Presigner.new
presigner.presigned_url(
:get_object,
bucket: ENV['S3_BUCKET'],
key: ENV['S3_KEY'],
expires_in: 1.day.to_i
).to_s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.