-2

I'm looking for a programmatic way to download images from an S3 bucket to my computer.

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.

This is my download function using the AWS documentation:

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.

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'
tblev
  • 422
  • 4
  • 13
  • Welcome to SO. A quick search reveals https://aws.amazon.com/blogs/developer/downloading-objects-from-amazon-s3-using-the-aws-sdk-for-ruby/ and https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/s3-example-get-bucket-item.html What do you mean "not my codebase"? – the Tin Man Apr 22 '20 at 16:34
  • Also, https://stackoverflow.com/q/8659382/128421 contains CLI solutions that are easily wrapped and scripted in Ruby. – the Tin Man Apr 22 '20 at 16:59
  • @theTinMan I'm not looking for wget, that downloads it to my codebase. The linked documentation says this: `obj.get(response_target: './my-code/my-item.txt')`. that's putting the item into my codes repository. – tblev Apr 22 '20 at 17:28

1 Answers1

1

If you change the disposition to attachment the browser download the file?

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

AndreDurao
  • 5,600
  • 7
  • 41
  • 61
  • Please don't ask questions in answers; Instead ask in a comment. Answers shouldn't stimulate conversations, they provide solutions. Also, if you suspect the solution is in another question or a different site, then you should mark the question as a duplicate or off-topic, respectively. – the Tin Man Apr 22 '20 at 16:24
  • Beautiful answer my friend. This is my first time working with S3 completely...I followed a couple of guides but wanted to stay away from the client. It works great now. Appending working code to answer. – tblev Apr 22 '20 at 17:23