Zero-byte files uploaded to S3 #1096

jasonfb opened this Issue Nov 29, 2012 · 2 comments


None yet
2 participants

jasonfb commented Nov 29, 2012

I'm using paperclip 3.3.1, aws-sdk 1.5.8, and Ruby 1.9.3-p194 on a Rails 3.1.8 project

When I upload a file, the file goes up to Amazon S3 in the right place, but it has no contents (zero-bytes) I can tell this because when I download it , it is empty. Also the file size is 0 bytes.

In the log, I see this:

[paperclip] saving inventory_csv_files/42/HOLfirst.csv
[AWS S3 200 0.579688 0 retries] put_object(:acl=>"public-read",:bucket_name=>"candidev",:cache_control=>"max-age=315576000",:content_type=>"text/csv",:data=>Paperclip::UploadedFileAdapter: HOLfirst.csv,:expires=>"Tue, 29 Nov 2022 17:37:46 GMT",:key=>"inventory_csv_files/42/HOLfirst.csv")
[paperclip] Saving attachments.

I checked the contents of the tempfile (which in my case happened to be /tmp/RackMultipart20121129-3054-1mfna4c) and they look like the file I put into the interface.

my has_attached_file is defined like so:

:path => "inventory_csv_files/:id/:basename.:extension",
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:s3_headers => { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate })

Not sure where to begin debugging this, anyone seen this kind of a thing before?

jasonfb commented Nov 29, 2012

Took me all morning but I nailed this. It was a problem in our code. Posting for everyone else's benefit.

Basically we had a after_create filter on the model that did a safety check of the CSV file uploaded -- we check for things like the right data format, the right number of columns.

In my case, I was doing this to get the contents of the CSV into memory

raw_data = CSV.parse(self.attachment.queued_for_write[:original].read)

Problem was that this put the read file pointer at the end of the file, so when Paperclip came along to read the file, it was already read from. Was able to easily fix this by calling this afterwards:


jasonfb closed this Nov 29, 2012

@jasonfb thanks for posting your solution. I just ran into a very similar issue, and was pulling my hair out trying to figure out what was going on. Rewind did the trick!

jasonfb referenced this issue in solidusio/solidus May 2, 2017


Error reading amazon s3 images in solidus 2.2 #1865

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment