使用AWS-SDK从S3下载文件。编码不正确 [英] Using aws-sdk to download files from s3. Encoding not right

查看:991
本文介绍了使用AWS-SDK从S3下载文件。编码不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AWS-SDK加载S3文件到本地磁盘,并质疑为什么我的PDF文件(只是有一个短信说样本PDF)原来有一个明显的空内容。

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?

下面是我的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

如果我走了编码:ASCII-8位,我只是得到一个错误信息编码:: UndefinedConversionError:\ XC3从ASCII-8BIT为UTF-8

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

推荐答案

一些研究,并从我的一个表弟小费后,我终于得到了这个工作。

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

(这是产生一个奇怪的PDF文件:显然等于原始,但与空白的内容,和Adobe Reader打开时,修复的话) 我,而不是现在使用的开放式的URI,使用SSL忽略。

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.

下面是最后的code这让我的日子。

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

这篇关于使用AWS-SDK从S3下载文件。编码不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆