我正在尝试使用 aws-sdk 将 s3 文件加载到本地磁盘,并质疑为什么我的 pdf 文件(其中只有一个文本说 SAMPLE PDF)的内容显然是空的。
我想这与编码有关...但是我该如何解决呢?
这是我的代码:
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
如果我取出encoding: "ascii-8bit"
,我只会收到一条错误消息Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8