简体   繁体   English

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

[英]How to download objects from S3 using AWS SDK - RESTful

I'm looking for a programmatic way to download images from an S3 bucket to my computer.我正在寻找一种将图像从 S3 存储桶下载到我的计算机的编程方式。

I tried " Using send_file to download a file from Amazon S3? " but it just redirected me to a link that only shows my PDF object.我尝试“ 使用 send_file 从 Amazon S3 下载文件? ”但它只是将我重定向到一个仅显示我的 PDF object 的链接。

This is my download function using the AWS documentation :这是我使用AWS 文档下载的 function :

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

and this is my download code from linked:这是我从链接下载的代码:

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 is what is confusing: I want to download it to my computer, not my codebase. obj.get令人困惑:我想将它下载到我的计算机,而不是我的代码库。

Some say that a download link is just the image link from my bucket if it is set to public, which it is.有人说下载链接只是我存储桶中的图像链接,如果它设置为公共,它就是这样。 What's this about?这是怎么回事?

Final solution: Change the disposition to 'attachment'最终解决方案:将处置更改为“附件”

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'

If you change the disposition to attachment the browser download the file?如果您将配置更改为attachment浏览器下载文件?

This may be related to content-disposition inline value.这可能与内容处置内联值有关。

Did you checked this question?你检查过这个问题吗? https://superuser.com/questions/1277819/why-does-chrome-sometimes-download-a-pdf-instead-of-opening-it https://superuser.com/questions/1277819/why-does-chrome-sometimes-download-a-pdf-instead-of-opening-it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM