Reputation: 6367
I am generating invoices as PDFs and want to upload them directly to S3.
I am using Wicked-PDF and the official AWS SDK.
gem 'wicked_pdf'
gem 'aws-sdk-s3', '~> 1'
Now I create the PDF:
pdf = render_to_string pdf: "some_file_name", template: "invoices/download", encoding: "UTF-8"
And want to upload it:
s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
obj = s3.bucket('bucket-development').object('Filename')
obj.upload_file(pdf)
The error I get:
ArgumentError: string contains null byte
If I store the PDF first to a defined path and use the save_path it works:
save_path = Rails.root.join('public','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
But I would like to upload he temporary PDF directly to S3 without saving the PDF first to my public folder.
Upvotes: 3
Views: 1282
Reputation: 622
The upload_file
method from AWS S3 SDK is working with files - see the method's description.
For uploading an object from memory, you should use the put
method - see the method's description in the 2nd way of uploading on this page
Upvotes: 3