# Aws::s3 to replace mysql blobs

**URL:** <https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676>\
**Category:** Rails\
**Created:** [September 28, 2010, 2:16am UTC](https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676 "2010-09-28T02:16:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 28, 2010, 2:16am UTC](https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676/1 "2010-09-28T02:16:34Z")

</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:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 28, 2010, 3:48am UTC](https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676/2 "2010-09-28T03:48:26Z")

</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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 28, 2010, 4:29am UTC](https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676/3 "2010-09-28T04:29:24Z")

</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:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 28, 2010, 4:56am UTC](https://www.ruby-forum.com/t/aws-s3-to-replace-mysql-blobs/195676/4 "2010-09-28T04:56:19Z")

</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.
