如何使用AWS SDK从S3下载对象--RESTful。

问题描述 投票:0回答:1

我正在寻找一种程序化的方式来下载S3桶中的图片到我的电脑上。

我试过"使用send_file从Amazon S3下载文件?",但它只是将我重定向到一个只显示我的PDF对象的链接。

这是我的下载函数,使用 AWS文档:

s3 = Aws::S3::Resource.new(region: 'us-east-2')
obj = s3.bucket('bucket').object('image')
obj.get(response_target: '~/')

这是我从链接上下载的代码。

data = open("https://#{bucket}.s3.amazonaws.com/#{filepath}")
send_data data.read, filename: "file.pdf", type: "application/pdf", disposition: 'inline', stream: 'true', buffer_size: '4096'

obj.get 是什么让我困惑。 我想把它下载到我的电脑上,而不是我的代码库。

有人说,下载链接只是我的代码库中的图片链接,如果它被设置为public,它就是这样。这到底是怎么回事?

最终的解决方案。改成 "附件

data = open("https://#{bucket}.s3.amazonaws.com/#{filepath}")
send_data data.read, filename: "file.pdf", type: "application/pdf", disposition: 'attachment', stream: 'true', buffer_size: '4096'
ruby-on-rails ruby amazon-web-services amazon-s3
1个回答
1
投票

如果你把处置方式改为 attachment 浏览器能否下载文件?

这可能与内容处置内联值有关。

你检查过这个问题吗?https:/superuser.comquestions1277819why-does-chrome-sometimes-download-a-pdf-instead-of-opening-it?

© www.soinside.com 2019 - 2024. All rights reserved.