Class: SimpleS3
- Inherits:
-
Object
- Object
- SimpleS3
- Defined in:
- lib/simple-s3/version.rb,
lib/simple-s3/simple-s3.rb
Constant Summary collapse
- VERSION =
"1.0.20"
Class Method Summary collapse
- .cloudfront_distribution_id ⇒ Object
- .config ⇒ Object
- .default_exclude_files ⇒ Object
- .get_files(config) ⇒ Object
- .invalidate! ⇒ Object
- .run! ⇒ Object
- .s3_access_key ⇒ Object
- .s3_bucket ⇒ Object
- .s3_bucket_endpoint ⇒ Object
- .s3_secret_key ⇒ Object
- .upload! ⇒ Object
Class Method Details
.cloudfront_distribution_id ⇒ Object
32 33 34 |
# File 'lib/simple-s3/simple-s3.rb', line 32 def self.cloudfront_distribution_id self.config['cloudfront_distribution_id'] end |
.config ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/simple-s3/simple-s3.rb', line 3 def self.config if @config.nil? path = './simple-s3.yml' raise "Simple-S3: Config file not found #{path}" unless File.exist?(path) @config = YAML.load_file(path) end @config end |
.default_exclude_files ⇒ Object
12 13 14 |
# File 'lib/simple-s3/simple-s3.rb', line 12 def self.default_exclude_files ['simple-s3.yml', '.git', '.rvmrc', 'Gemfile', 'Gemfile.lock', '.gitignore', '.gitmodules'] end |
.get_files(config) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/simple-s3/simple-s3.rb', line 41 def self.get_files(config) exclude = config['exclude_files'] || [] exclude |= self.default_exclude_files inc = config['include_files'] || './**/*' inc = [inc] unless inc.is_a?(Array) files = [] inc.each do |ii| Dir[ii].each do |file| path = File.(file) next if File.directory?(path) name = File.basename(file) found = false exclude.each do |ex| found = true if file.start_with?(ex) || ex == name end files.push(file) unless found end end files end |
.invalidate! ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/simple-s3/simple-s3.rb', line 109 def self.invalidate! distribution = self.cloudfront_distribution_id return if distribution.nil? aws_account = self.s3_access_key aws_secret = self.s3_secret_key path = ['/','/*','/**/*'] date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z") digest = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), aws_secret, date)).strip uri = URI.parse("https://cloudfront.amazonaws.com/2012-07-01/distribution/#{distribution}/invalidation") path_xml = '' path.each do |p| path_xml += '<Path>'+p+'</Path>' end req = Net::HTTP::Post.new(uri.path) req.initialize_http_header({ 'x-amz-date' => date, 'Content-Type' => 'text/xml', 'Authorization' => "AWS %s:%s" % [aws_account, digest] }) xml = '<InvalidationBatch xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">' + ' <Paths>' + ' <Quantity>' + path.length.to_s + '</Quantity>' + ' <Items>' + path_xml + ' </Items>' + ' </Paths>' + ' <CallerReference>SOMETHING_SPECIAL_' + Time.now.utc.to_i.to_s + '</CallerReference>' + '</InvalidationBatch>' req.body = xml http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE res = http.request(req) # it was successful if response code was a 201 if res.code == '201' puts 'Simple-S3: Invalidation Successful' else raise res.body.to_s end end |
.run! ⇒ Object
36 37 38 39 |
# File 'lib/simple-s3/simple-s3.rb', line 36 def self.run! self.upload! self.invalidate! end |
.s3_access_key ⇒ Object
16 17 18 |
# File 'lib/simple-s3/simple-s3.rb', line 16 def self.s3_access_key self.config['s3_access_key'] end |
.s3_bucket ⇒ Object
24 25 26 |
# File 'lib/simple-s3/simple-s3.rb', line 24 def self.s3_bucket self.config['s3_bucket'] end |
.s3_bucket_endpoint ⇒ Object
28 29 30 |
# File 'lib/simple-s3/simple-s3.rb', line 28 def self.s3_bucket_endpoint self.config['s3_bucket_endpoint'] end |
.s3_secret_key ⇒ Object
20 21 22 |
# File 'lib/simple-s3/simple-s3.rb', line 20 def self.s3_secret_key self.config['s3_secret_key'] end |
.upload! ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/simple-s3/simple-s3.rb', line 65 def self.upload! bucket = self.s3_bucket raise 'Simple-S3: Bucket not defined' if bucket.length == 0 = config['metadata'] || {} [:access] ||= 'public-read' files = self.get_files(config) raise 'Simple-S3: No files found' if files.length == 0 new_endpoint = self.s3_bucket_endpoint.to_s if new_endpoint.length > 0 old_endpoint = AWS::S3::DEFAULT_HOST.to_s puts "Simple-S3: Changing the bucket endpoint from '#{old_endpoint}' to '#{new_endpoint}'" AWS::S3::DEFAULT_HOST.replace(new_endpoint) end = { :access_key_id => self.s3_access_key, :secret_access_key => self.s3_secret_key } begin AWS::S3::Base.establish_connection!() rescue Exception => e raise 'Simple-S3: Unable to establish connection to AWS (this may because of an invalid bucket name)' end files.each do |file| path = File.(file) final_file = file.gsub(/^\.\//, '') puts "Simple-S3: Uploading #{file} as '#{final_file}' to '#{bucket}'" AWS::S3::S3Object.store( final_file, File.open(path), bucket, ) end puts "Simple-S3: Upload Completed!" end |