Question

I'm working on a Rails website that uses Paperclip to upload files to Amazon S3 and then serve them through Cloudfront. I've got the uploading working fine, and Cloudfront is serving the files properly, but for some reason ModelObj.attachment.url isn't working properly on my production server. It works fine on my development server (WEBrick under Windows 8) but is returning the wrong URL on the live site (LAMP with Phusion Passenger). It returns almost the right url, but instead of

http://[stuff].cloudfront.net/kidbooks/snds/5072_original.mp3?1393858446

I'm getting

http:///kidbooks/snds/5072_original.mp3?1393858446

It's configured as follows: In environment.rb:

Paperclip::Attachment.default_options.merge!({
  :storage => :s3,
  :bucket => APP_CONFIG['s3_bucket'],
  :path => "/#{APP_CONFIG['s3_path']}/:attachment/:id_:style.:extension",
  :s3_credentials => {
    ...
  }
})

And in the model:

has_attached_file :snd,
  :url => ':s3_alias_url',
  :s3_host_alias => APP_CONFIG['cloudfront_domain'] 

APP_CONFIG is being set properly on both servers; I've checked. (Or at least, it's being set properly in the console on the live server; I guess I could check more directly. There doesn't appear to be anything relevant in environments/development.rb or production.rb. What's going wrong here, and how can I fix it?

(I found this question, which is close to what I need, but only involves S3.)

Update: Found something weird. Model.snd.options returns a hash that includes, among other things, :s3_host_alias=>nil. Maybe my model is somehow being loaded before my configuration files?

Was it helpful?

Solution

Aha, found it. For some reason my model is either being loaded before my config files, or the loader doesn't have access to APP_CONFIG. This meant that :s3_host_alias was being set to nil, which of course broke things. I moved :s3_host_alias => APP_CONFIG['cloudfront_domain'] from the model to environment.rb, and now it's working fine.

I have no idea why my model would be loading before my config files, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top