在编码'utf-8'并从amazon-s3存储桶中读取文件时遇到问题。
我创建了一个文件。
file = File.open('new_file', 'w', :encoding => 'utf-8')
string = "Some ££££ sings"
file.write(string)
file.close
从本地阅读时,一切正常。
open('new_file').read
=> "Some ££££ sings"
现在我使用aws_s3将文件上传到amazon s3。
AWS::S3::S3Object.store('new_file', open('new_file'), 'my_bucket')
=> #<AWS::S3::S3Object::Response:0x2214462560 200 OK>
当我从亚马逊s3阅读时
AWS::S3::S3Object.find('new_file', 'my_bucket').value
=> "Some \xC2\xA3\xC2\xA3\xC2\xA3\xC2\xA3 sings"
open(AWS::S3::S3Object.find('new_file','my_bucket').url).read
=> "Some \xC2\xA3\xC2\xA3\xC2\xA3\xC2\xA3 sings"
我一直在尝试许多仍然无法解决的问题。
中号
答案 0 :(得分:3)
我在不同的论坛上找到了解决方案。
他们这样做是为了确保我们首先在'utf-8'中传递/上传文本文件。它本身不会解决问题,但会允许您确定强制返回字符串编码。
open(AWS::S3::S3Object.find('new_file','my_bucket').url).read.force_encoding('utf-8')
答案 1 :(得分:0)
我认为有更好的解决方案。将您要写入的文件放在 binmode 。
中file = File.open("test.txt", "wb")
# or use File#binmode
file = File.open("test.txt")
file.binmode
# binmode also works with Tempfile
file = Tempfile.new
file.binmode
# then proceed to downloading
s3 = AWS::S3.new
s3.buckets["foo"]["test.txt"].read do |chunk|
file.write(chunk)
end