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

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

Answers


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

Need Your Help

bash + verify results from output

linux bash shell awk

I need to find if resource is not online according to the following rules

Generate ER diagram from postgres-sql database [closed]

sql postgresql reverse-engineering

Could you tell me if you know any programs that can generate ER-diagrams from an existing postgres-sql database on osx snow leopard?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.