您可以使用 s3 分段上传,它允许通过将大对象拆分为多个块进行上传。
https://docs.aws.amazon.com/AmazonS3/latest/dev/mpuoverview.html
分段上传需要更复杂的编码,但 aws-sdk-ruby V3 支持upload_stream 方法,该方法似乎在内部执行分段上传,并且非常易于使用。也许这个用例的确切解决方案。
https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#upload_stream-instance_method
client = Aws::S3::Client.new(
region: 'ap-northeast-1',
credentials: your_credential
)
obj = Aws::S3::Object.new('your-bucket-here', 'path-to-output', client: client)
require "csv"
obj.upload_stream do |write_stream|
[
%w(this is first line),
%w(this is second line),
%w(this is third line),
].each do |line|
write_stream << line.to_csv
end
end
this,is,first,line
this,is,second,line
this,is,third,line
upload_stream 块的参数通常可以用作 IO 对象,它允许您像处理文件或其他 IO 对象一样链接和包装 CSV 生成:
obj.upload_stream do |write_stream|
CSV(write_stream) do |csv|
[
%w(this is first line),
%w(this is second line),
%w(this is third line),
].each do |line|
csv << line
end
end
end
或者,例如,您可以在生成和上传 CSV 时对其进行压缩,使用临时文件来减少内存占用:
obj.upload_stream(tempfile: true) do |write_stream|
# When uploading compressed data, use binmode to avoid an encoding error.
write_stream.binmode
Zlib::GzipWriter.wrap(write_stream) do |gzw|
CSV(gzw) do |csv|
[
%w(this is first line),
%w(this is second line),
%w(this is third line),
].each do |line|
csv << line
end
end
end
end
已编辑:在压缩的示例代码中,您必须添加binmode 以修复以下错误:
Aws::S3::MultipartUploadError: multipart upload failed: "\x8D" from ASCII-8BIT to UTF-8