Aws::s3 to replace mysql blobs

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

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

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs