# aws::s3 to replace mysql blobs

**URL:** <https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698>\
**Category:** rubyonrails-talk\
**Created:** [September 28, 2010, 12:15am UTC](https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698 "2010-09-28T00:15:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@giorgio](https://discuss.rubyonrails.org/u/giorgio)\
**Post date:** [September 28, 2010, 12:15am UTC](https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698/1 "2010-09-28T00:15:26Z")

</div>

Hi,

I have used attachment\_fu before with no real problems but I'm not sure if it is what I need now..

I have an application that stores blobs (word documents, scanned documents etc) in the database. I want to convert it to store in S3.

All the paperclip and attachment\_fu examples deal with images and resizing and thumbnails etc etc which are not relevent in this case.

Are they still the best options or should I just use the aws-s3 gem on its own?

Ideally I'd like to just replace the database "content" column by saving to S3 as part of an AR callback and have an accessor on the model that retrievs the attachment only when requested...

Anybody got any examples of something like that.

Cheers George

---

<div class="post-metadata">

**Author:** ![DK11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@DK11](https://discuss.rubyonrails.org/u/DK11)\
**Post date:** [September 28, 2010, 1:47am UTC](https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698/2 "2010-09-28T01:47:06Z")

</div>

George - I did the exact thing a few months ago - actually for a test environment as I deal with credit data and cant put live data on S3. Anyhow, I tried and really liked paperclip, but ended up just going directly with the s3 gem. I thing there were some complications with how I wanted to be able to both save and modify files which I found easier going with S3 gem directly. Anyhow, here are my functions…

def self.save\_file(file\_path, data) begin if USE\_AMAZON\_S3 require ‘aws/s3’ file\_path = file\_path.gsub(FILESTORE, “”) # remove the path to filestore (leaving the app as is since S3 is for test only) #file\_path = file\_path.gsub(/^//, “”) AWS::S3::Base.establish\_connection!( :access\_key\_id =\> ‘zzzzz’, :secret\_access\_key =\> ‘zzzzz’ ) AWS::S3::S3Object.store(file\_path, data, AMAZON\_S3\_BUCKET) return true if AWS::S3::Service.response.success? else FileUtils.mkdir\_p GlobalFunctions.get\_path\_without\_file\_name(file\_path) chars\_written = File.open(file\_path, ‘w’) {|f| f.write(data) } return true if chars\_written == data.length end rescue SystemError.new(:user\_id =\> nil, :account\_id =\> nil, :location =\> “GlobalFunctions.save\_file”, :error =\> “failed to save file”, :incidentals =\> {“file\_path” =\> file\_path, “data” =\> data} ).save end false end

def self.load\_file\_data(path) begin if USE\_AMAZON\_S3 require ‘aws/s3’ path = path.gsub(FILESTORE, “”) # remove the path to filestore (leaving the app as is since S3 is for test only) AWS::S3::Base.establish\_connection!( :access\_key\_id =\> ‘zzzzz’, :secret\_access\_key =\> ‘zzzzz’ ) return AWS::S3::S3Object.value path, AMAZON\_S3\_BUCKET else return File.open(path, “r”).read end rescue SystemError.new(:user\_id =\> nil, :account\_id =\> nil, :location =\> “GlobalFunctions.load\_file\_data”, :error =\> “failed to read file”, :incidentals =\> {“path” =\> path} ).save end nil end

---

<div class="post-metadata">

**Author:** ![giorgio](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@giorgio](https://discuss.rubyonrails.org/u/giorgio)\
**Post date:** [September 28, 2010, 2:28am UTC](https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698/3 "2010-09-28T02:28:08Z")

</div>

Thanks for that I'll check that out to see if it fits my needs..

One thing I was unsure about was when to do the "establish\_connection". I see you do it as part of the actual save...

G.

---

<div class="post-metadata">

**Author:** ![DK11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@DK11](https://discuss.rubyonrails.org/u/DK11)\
**Post date:** [September 28, 2010, 2:55am UTC](https://discuss.rubyonrails.org/t/aws-s3-to-replace-mysql-blobs/54698/4 "2010-09-28T02:55:23Z")

</div>

Yeah… that was all trial an error on the ruby console. Wasn’t too bad but as always these things can be an adventure. Do note that you probably want to put your S3 auth info somewhere else/safer than I did it.
