我正在使用 wicked_pdf 生成 pdf 文件并想将其上传到 s3 存储桶,因为 heroku 不公开保存。但面临如何上传到那里或如何在保存路径中提供 s3 存储桶路径的问题。
这是我的 Controller 代码:-
pdf = render_to_string :pdf => "user_detail"
# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
最佳答案
除了Anurag Aryan回答你可以使用Aws::S3::Object#put .如果对象是字符串或不是磁盘上文件的 IO 对象,这将很有用:
filename = 'filename.pdf'
pdf = render_to_string :pdf => "user_detail"
s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
obj = s3.bucket('your-s3-bucket-name').object(filename)
# upload pdf data as a string
obj.put(body: pdf)
# or as an IO object
save_path = Rails.root.join('pdfs', filename)
File.open(save_path, 'wb') do |file|
file << pdf
obj.put(body: file)
end
关于ruby-on-rails - 如何将 Controller 中生成的pdf文件上传到S3存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49841227/