Using aws-sdk to download files from s3. Encoding not right

Deadly 提交于 2020-01-15 11:28:08

问题


I am trying to use aws-sdk to load s3 files to local disk, and question why my pdf file (which just has a text saying SAMPLE PDF) turns out with an apparently empty content.

I guess it has something to do with the encoding...but how can i fix it?

Here is my code :

require 'aws-sdk'

bucket_name = "****"
access_key_id = "***"
secret_access_key = "**"

s3=AWS::S3.new(
access_key_id: access_key_id,
secret_access_key: secret_access_key)

b = s3.buckets[bucket_name]
filen = File.basename("Sample.pdf")

path = "original/90/#{filen}"
o = b.objects[path]

require 'tempfile'

ext= File.extname(filen)

file = File.open("test.pdf","w", encoding: "ascii-8bit")
# streaming download from S3 to a file on disk

begin
file.write(o.read) do |chunk|
    file.write(chunk)
end
end
file.close

If i take out the encoding: "ascii-8bit", i just get an error message Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8


回答1:


After some research and a tip from a cousin of mine, i finally got this to work.

Instead of using the aws solution to load the file from amazon and write it to disk (which was generating a strange pdf file : apparently equal to the original, but with blank content, and Adobe Reader "fixing" it when opening) i instead am now using open-uri, with SSL ignore.

Here is the final code which made my day :

require 'open-uri'
open('test.pdf', 'wb') do |file|
  file << open('https://s3.amazon.com/mybucket/Sample.pdf',:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
end


来源:https://stackoverflow.com/questions/12352230/using-aws-sdk-to-download-files-from-s3-encoding-not-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!