Module: ModelAttachment::AmazonInstanceMethods
- Defined in:
- lib/model_attachment/amazon.rb
Instance Attribute Summary collapse
-
#default_bucket ⇒ Object
Returns the value of attribute default_bucket.
Instance Method Summary collapse
-
#aws_connect ⇒ Object
connect to aws, uses access_key_id and secret_access_key in config/amazon.yml.
-
#aws_key(type = "") ⇒ Object
creates the aws_key
type: type passed to has_attachment, ex. -
#aws_url(type = "") ⇒ Object
returns the aws url
type: type passed to has_attachment, ex. -
#move_to_amazon ⇒ Object
moves file to amazon along with modified images, removes local images once object existence is confirmed.
-
#move_to_filesystem ⇒ Object
moves files back to local filesystem, along with modified images, removes from amazon once they are confirmed locally.
-
#remove_from_amazon ⇒ Object
removes files from amazon.
Instance Attribute Details
#default_bucket ⇒ Object
Returns the value of attribute default_bucket.
3 4 5 |
# File 'lib/model_attachment/amazon.rb', line 3 def default_bucket @default_bucket end |
Instance Method Details
#aws_connect ⇒ Object
connect to aws, uses access_key_id and secret_access_key in config/amazon.yml
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/model_attachment/amazon.rb', line 27 def aws_connect log("aws_connect") return if AWS::S3::Base.connected? begin config = YAML.load_file(self.class.[:aws]) if config AWS::S3::Base.establish_connection!( :access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'], :use_ssl => true, :persistent => true # if issues with disconnections, set to false ) self.default_bucket = self.class.[:bucket] || 'globalfolders' else raise "You must provide an amazon.yml config file" end rescue AWS::S3::ResponseError => error log("Could not connect to amazon: #{error.message}") end end |
#aws_key(type = "") ⇒ Object
creates the aws_key type: type passed to has_attachment, ex. small, large
21 22 23 24 |
# File 'lib/model_attachment/amazon.rb', line 21 def aws_key(type = "") file = (type.nil? || type == "" ? filename : basename + "_" + type + extension) (path + file).gsub!(/^\//,'') end |
#aws_url(type = "") ⇒ Object
returns the aws url type: type passed to has_attachment, ex. small, large
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/model_attachment/amazon.rb', line 7 def aws_url(type = "") aws_connect begin return AWS::S3::S3Object.find(aws_key(type), default_bucket).url rescue Exception => e log("Amazon: #{e.message}") log("Backtrace: #{e.backtrace[0]}") log("Could not get object: #{aws_key(type)}") end end |
#move_to_amazon ⇒ Object
moves file to amazon along with modified images, removes local images once object existence is confirmed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/model_attachment/amazon.rb', line 50 def move_to_amazon log("Move #{aws_key} to Amazon.") aws_connect begin AWS::S3::S3Object.store(aws_key, open(full_filename, 'rb'), default_bucket, :content_type => content_type) # copy over modified files process_image_types do |name, value| AWS::S3::S3Object.store(aws_key(name.to_s), open(full_filename, 'rb'), default_bucket, :content_type => content_type) end self.bucket = self.default_bucket @dirty = true save! rescue AWS::S3::ResponseError => error log("Store Object Failed: #{error.message}") rescue StandardError => e log("Move to Amazon Failed: #{e.message}") end begin if AWS::S3::S3Object.exists?(aws_key, default_bucket) log("Remove Filename #{full_path + filename}") FileUtils.rm(full_path + filename) end # remove any modified local files process_image_types do |name, value| if AWS::S3::S3Object.exists?(aws_key(name.to_s), default_bucket) log("Remove Filename #{full_path + filename(name)}") FileUtils.rm(full_path + filename(name.to_s)) end end rescue AWS::S3::ResponseError => error log("Could not check objects existence: #{error.message}") rescue StandardError => error log("Removing file failed: #{error.message}.") end end |
#move_to_filesystem ⇒ Object
moves files back to local filesystem, along with modified images, removes from amazon once they are confirmed locally
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/model_attachment/amazon.rb', line 92 def move_to_filesystem aws_connect begin # streaming causes encoding error File.open(full_filename, 'wb') do |file| file.write(AWS::S3::S3Object.value(path + file_name, default_bucket)) file.rewind # AWS::S3::S3Object.stream(path + file_name, default_bucket) do |chunk| # log("Encoding: #{chunk.encoding}") # file.write chunk # end end # copy over modified files process_image_types do |name, value| File.open(full_filename(name), 'wb') do |file| file.write(AWS::S3::S3Object.value(path + file_name, default_bucket)) file.rewind # AWS::S3::S3Object.stream(path + filename(name), default_bucket) do |chunk| # file.write chunk # end end end rescue AWS::S3::ResponseError => error log("Copying File to local filesystem failed: #{error.message}") end if File.size(full_filename) == file_size remove_from_amazon end end |
#remove_from_amazon ⇒ Object
removes files from amazon
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/model_attachment/amazon.rb', line 131 def remove_from_amazon begin log("Removing #{aws_key} from Amazon") object = AWS::S3::S3Object.find(aws_key, default_bucket) object.delete # remove modified files process_image_types do |name, value| AWS::S3::S3Object.find(aws_key(name.to_s), default_bucket).delete end # make sure we set the bucket to nil so we know they're local self.bucket = nil @dirty = true save! rescue AWS::S3::ResponseError => error log("Removing file from amazon failed: #{error.message}") rescue StandardError => e log("Failed remove from Amazon: #{e.message}") end end |