Skip to content
Browse files

Extract get_from_s3 into library.

This makes it possible to get files from s3 outside the normal dsl.
  • Loading branch information...
1 parent cb455c7 commit ae19c1356f7255e1fb6cc7989b2fea258e843375 @trotter committed Apr 18, 2012
Showing with 22 additions and 19 deletions.
  1. +21 −0 libraries/s3_file.rb
  2. +1 −19 providers/default.rb
View
21 libraries/s3_file.rb
@@ -0,0 +1,21 @@
+require 'rest-client'
+require 'time'
+require 'openssl'
+require 'base64'
+
+module S3File
+ def get_from_s3(bucket,path,aws_access_key_id,aws_secret_access_key)
+ now = Time.now().utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
+ string_to_sign = "GET\n\n\n%s\n/%s%s" % [now,bucket,path]
+
+ digest = digest = OpenSSL::Digest::Digest.new('sha1')
+ signed = OpenSSL::HMAC.digest(digest, aws_secret_access_key, string_to_sign)
+ signed_base64 = Base64.encode64(signed)
+
+ auth_string = 'AWS %s:%s' % [aws_access_key_id,signed_base64]
+
+ response = RestClient.get('https://%s.s3.amazonaws.com%s' % [bucket,path], :date => now, :authorization => auth_string)
+
+ return response.body
+ end
+end
View
20 providers/default.rb
@@ -1,7 +1,4 @@
-require 'rest-client'
-require 'time'
-require 'openssl'
-require 'base64'
+include S3File
action :create do
body = get_from_s3(@new_resource.bucket, @new_resource.remote_path, @new_resource.aws_access_key_id, @new_resource.aws_secret_access_key).body
@@ -11,18 +8,3 @@
end
end
-def get_from_s3(bucket,path,aws_access_key_id,aws_secret_access_key)
- now = Time.now().utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
- string_to_sign = "GET\n\n\n%s\n/%s%s" % [now,bucket,path]
-
- digest = digest = OpenSSL::Digest::Digest.new('sha1')
- signed = OpenSSL::HMAC.digest(digest, aws_secret_access_key, string_to_sign)
- signed_base64 = Base64.encode64(signed)
-
- auth_string = 'AWS %s:%s' % [aws_access_key_id,signed_base64]
-
- response = RestClient.get('https://%s.s3.amazonaws.com%s' % [bucket,path], :date => now, :authorization => auth_string)
-
- return response.body
-end
-

0 comments on commit ae19c13

Please sign in to comment.
Something went wrong with that request. Please try again.