Alex Rivera | Logout

How to use Paperclip without a file, just with a string?

Asked 2010-11-03T15:55:04.813
15

I have a rake file, that reads content via HTTP and I want to use Paperclip to store the loaded content on Amazon S3. It works fine when I provide a local file, but I would like to set the content as a string and set the content type manually.

The following does not work. No error is issued, the database entry is updated, but no file is created in S3:

p.attachment = "Test"
p.attachment_file_name = "test.txt"
p.attachment_content_type = "text/plain"
p.attachment_file_size = "Test".size
p.attachment_updated_at = Time.now
p.save

I guess I could write a temporary file with my content, but that would be a pretty inefficient solution.

Edit
Report

1 Answer

44

To avoid littering the filesystem with temp files, you can use StringIO as in:

p.attachment = StringIO.new(your_string)
answered 2013-03-19T19:51:30.233

Your Answer