AttachmentFu Pull S3 From Production Bucket While Developing
Populating a database is a chore. As soon as a project gets deployed and the prospective admins are swarming about inputting data, my routine of downloading the production database chock-full of tasty data begins. During development most of my data was actually produced in production.
Normally this is a seamless process automated by a cut and paste snippet. But with AttachmentFu using the S3 backend, the bucket where files are stored is implict based on the configuration for your particular environment. There are a few potential solutions:
- Share buckets between environments. This is the easiest, but also quite risky with regards to data integrity—if you follow a policy of only deleting attachments from the primary environment this can work, however there is still a slight risk of overwriting data with the same name.
- Copy data between buckets. This is sort of a pain. The logic can be quite simple though, and if you have an EC2 instance set up it is pretty fast.
- Hack the URL. The quick and dirty, but effective solution is to hack the URLs that are output to the browser so you can view the images from an alternate bucket without affecting uploads or deletions.
I want to do this locally without modifying the repository code, so what I did is add a new file containing the following to
config/initializers/ and add it to my local git ignores:
Technoweenie::AttachmentFu::Backends::S3Backend.module_eval do def s3_url(thumbnail = nil) File.join(s3_protocol + s3_hostname + s3_port_string, 'production_bucket', full_filename(thumbnail)) end alias :public_filename :s3_url end
The downside is that you won’t be able to see files that were uploaded from development. But for me that doesn’t matter because I only ever do that when I’m developing the image uploading forms, which soon are overwritten by production data.
Implementing this for secure urls is left as an exercise to the reader.