simple-s3 1.0.18 → 1.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,10 +29,6 @@ class SimpleS3
29
29
  self.config['s3_bucket_endpoint']
30
30
  end
31
31
 
32
- def self.s3_server
33
- self.config['s3_server']
34
- end
35
-
36
32
  def self.cloudfront_distribution_id
37
33
  self.config['cloudfront_distribution_id']
38
34
  end
@@ -77,19 +73,20 @@ class SimpleS3
77
73
 
78
74
  raise 'Simple-S3: No files found' if files.length == 0
79
75
 
80
- s3_options = {
76
+ new_endpoint = self.s3_bucket_endpoint.to_s
77
+ if new_endpoint.length > 0
78
+ old_endpoint = AWS::S3::DEFAULT_HOST.to_s
79
+ puts "Simple-S3: Changing the bucket endpoint from '#{old_endpoint}' to '#{new_endpoint}'"
80
+ AWS::S3::DEFAULT_HOST.replace(new_endpoint)
81
+ end
82
+
83
+ s3_auth_options = {
81
84
  :access_key_id => self.s3_access_key,
82
85
  :secret_access_key => self.s3_secret_key
83
86
  }
84
87
 
85
- endpoint = self.s3_bucket_endpoint.to_s
86
- if endpoint.length > 0
87
- puts "Simple-S3: Changing the bucket endpoint to: '#{endpoint}'"
88
- s3_options[:server] = endpoint
89
- end
90
-
91
88
  begin
92
- AWS::S3::Base.establish_connection!(s3_options)
89
+ AWS::S3::Base.establish_connection!(s3_auth_options)
93
90
  rescue Exception => e
94
91
  raise 'Simple-S3: Unable to establish connection to AWS (this may because of an invalid bucket name)'
95
92
  end
@@ -0,0 +1,158 @@
1
+ class SimpleS3
2
+
3
+ def self.config
4
+ if @config.nil?
5
+ path = './simple-s3.yml'
6
+ raise "Simple-S3: Config file not found #{path}" unless File.exist?(path)
7
+ @config = YAML.load_file(path)
8
+ end
9
+ @config
10
+ end
11
+
12
+ def self.default_exclude_files
13
+ ['simple-s3.yml', '.git', '.rvmrc', 'Gemfile', 'Gemfile.lock', '.gitignore', '.gitmodules']
14
+ end
15
+
16
+ def self.s3_access_key
17
+ self.config['s3_access_key']
18
+ end
19
+
20
+ def self.s3_secret_key
21
+ self.config['s3_secret_key']
22
+ end
23
+
24
+ def self.s3_bucket
25
+ self.config['s3_bucket']
26
+ end
27
+
28
+ <<<<<<< HEAD
29
+ def self.s3_bucket_endpoint
30
+ self.config['s3_bucket_endpoint']
31
+ =======
32
+ def self.s3_server
33
+ self.config['s3_server']
34
+ >>>>>>> 1326402ed8d3c30b23ca21c2c20d19337e6a9c8f
35
+ end
36
+
37
+ def self.cloudfront_distribution_id
38
+ self.config['cloudfront_distribution_id']
39
+ end
40
+
41
+ def self.run!
42
+ self.upload!
43
+ self.invalidate!
44
+ end
45
+
46
+ def self.get_files(config)
47
+ exclude = config['exclude_files'] || []
48
+ exclude |= self.default_exclude_files
49
+ inc = config['include_files'] || './**/*'
50
+ inc = [inc] unless inc.is_a?(Array)
51
+
52
+ files = []
53
+ inc.each do |ii|
54
+ Dir[ii].each do |file|
55
+ path = File.expand_path(file)
56
+ next if File.directory?(path)
57
+
58
+ name = File.basename(file)
59
+ found = false
60
+ exclude.each do |ex|
61
+ found = true if file.start_with?(ex) || ex == name
62
+ end
63
+ files.push(file) unless found
64
+ end
65
+ end
66
+
67
+ files
68
+ end
69
+
70
+ def self.upload!
71
+ bucket = self.s3_bucket
72
+ raise 'Simple-S3: Bucket not defined' if bucket.length == 0
73
+
74
+ metadata = config['metadata'] || {}
75
+ metadata[:access] ||= 'public-read'
76
+
77
+ endpoint = self.s3_bucket_endpoint.to_s
78
+ if endpoint.length > 0
79
+ puts "Simple-S3: Changing the bucket endpoint to: '#{endpoint}'"
80
+ AWS::S3::DEFAULT_HOST.replace(endpoint)
81
+ end
82
+
83
+ files = self.get_files(config)
84
+
85
+ raise 'Simple-S3: No files found' if files.length == 0
86
+
87
+ AWS::S3::Base.establish_connection!(
88
+ :access_key_id => self.s3_access_key,
89
+ :secret_access_key => self.s3_secret_key,
90
+ :server => self.s3_server
91
+ )
92
+
93
+
94
+ files.each do |file|
95
+ path = File.expand_path(file)
96
+ final_file = file.gsub(/^\.\//, '')
97
+ puts "Simple-S3: Uploading #{file} as '#{final_file}' to '#{bucket}'"
98
+ AWS::S3::S3Object.store(
99
+ final_file,
100
+ File.open(path),
101
+ bucket,
102
+ metadata
103
+ )
104
+ end
105
+
106
+ puts "Simple-S3: Upload Completed!"
107
+ end
108
+
109
+ def self.invalidate!
110
+ distribution = self.cloudfront_distribution_id
111
+ return if distribution.nil?
112
+
113
+ aws_account = self.s3_access_key
114
+ aws_secret = self.s3_secret_key
115
+
116
+ path = ['/','/*','/**/*']
117
+
118
+ date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
119
+ digest = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), aws_secret, date)).strip
120
+ uri = URI.parse("https://cloudfront.amazonaws.com/2012-07-01/distribution/#{distribution}/invalidation")
121
+
122
+ path_xml = ''
123
+ path.each do |p|
124
+ path_xml += '<Path>'+p+'</Path>'
125
+ end
126
+
127
+ req = Net::HTTP::Post.new(uri.path)
128
+ req.initialize_http_header({
129
+ 'x-amz-date' => date,
130
+ 'Content-Type' => 'text/xml',
131
+ 'Authorization' => "AWS %s:%s" % [aws_account, digest]
132
+ })
133
+
134
+ xml = '<InvalidationBatch xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">' +
135
+ ' <Paths>' +
136
+ ' <Quantity>' + path.length.to_s + '</Quantity>' +
137
+ ' <Items>' +
138
+ path_xml +
139
+ ' </Items>' +
140
+ ' </Paths>' +
141
+ ' <CallerReference>SOMETHING_SPECIAL_' + Time.now.utc.to_i.to_s + '</CallerReference>' +
142
+ '</InvalidationBatch>'
143
+ req.body = xml
144
+
145
+ http = Net::HTTP.new(uri.host, uri.port)
146
+ http.use_ssl = true
147
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
148
+ res = http.request(req)
149
+
150
+ # it was successful if response code was a 201
151
+ if res.code == '201'
152
+ puts 'Simple-S3: Invalidation Successful'
153
+ else
154
+ raise res.body.to_s
155
+ end
156
+ end
157
+
158
+ end
@@ -1,3 +1,3 @@
1
1
  class SimpleS3
2
- VERSION = "1.0.18"
2
+ VERSION = "1.0.20"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 18
9
- version: 1.0.18
8
+ - 20
9
+ version: 1.0.20
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matias Niemela
@@ -44,6 +44,7 @@ files:
44
44
  - bin/simple-s3
45
45
  - lib/simple-s3.rb
46
46
  - lib/simple-s3/simple-s3.rb
47
+ - lib/simple-s3/simple-s3.rb.orig
47
48
  - lib/simple-s3/version.rb
48
49
  - make_gem.sh
49
50
  - simple-s3.gemspec