dm-s3
Very often we want to store a file in s3 and keep a record of it in our database.
Setting
If you have a model class that will have s3 objects living in different buckets do
property :s3_key, String
property :s3_bucket, String
has_s3_file
and define a method to save your resource and your s3 object
def self.create!(key, bucket, file, )
object = self.new :s3_key => key, :s3_bucket => bucket
object.save if object.store_with(file, )
end
If all the s3 objects will live in the same bucket then do
property :s3_key, String
has_s3_file_at 'my-bucket-name'
The save method will reflect the difference
def self.create!(key, file, )
object = self.new :s3_key => key
object.save if object.store_with(file, )
end
You can directly access the bucket object associated with this model too
MyModel.bucket
Getting
To retrieve data from s3 simply pull the datamapper object, e.g.
object = MyModel.first
Doing
object.s3
Will give you the s3 object. So you can do things like
object.s3.value
object.s3.url
object.s3.about
etc.
For convenience, the methods ‘value’, ‘metadata’, ‘about’ and ‘url’ get bound to the datamapper object directly so the following is valid
object.value
object.
object.about
object.url
Make sure these don’t conflict with your properties.
Gotchas
-
The gem assumes that you will call
AWS::S3::Base.establish_connection!
with the appropriate credentials some time before you create or access your s3 enabled models
-
The gem won’t create buckets for you. Make sure the buckets exist before attempting to use them.
Copyright
Copyright © 2009 Roberto Thais. See LICENSE for details.