【发布时间】:2020-08-05 17:56:34
【问题描述】:
我正在寻找一种以编程方式将图像从 S3 存储桶下载到我的计算机的方法。
我尝试了“Using send_file to download a file from Amazon S3?”,但它只是将我重定向到一个仅显示我的 PDF 对象的链接。
这是我使用AWS documentation的下载功能:
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 令人困惑的是:我想将其下载到我的计算机,而不是我的代码库。
有人说下载链接只是我存储桶中的图像链接,如果它设置为公共,它就是。这是怎么回事?
最终解决方案:将处置更改为“附件”
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'
【问题讨论】:
-
欢迎来到 SO。快速搜索会发现 aws.amazon.com/blogs/developer/… 和 docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/… “不是我的代码库”是什么意思?
-
另外,stackoverflow.com/q/8659382/128421 包含 CLI 解决方案,这些解决方案很容易用 Ruby 封装和编写脚本。
-
@theTinMan 我不是在寻找 wget,它会将它下载到我的代码库中。链接的文档是这样说的:
obj.get(response_target: './my-code/my-item.txt')。这就是将项目放入我的代码存储库中。
标签: ruby-on-rails ruby amazon-web-services amazon-s3