Module: S3::Helpers
- Defined in:
- lib/s3/helpers.rb
Class Method Summary collapse
- .body_digest(body) ⇒ Object
- .build_query_string(params) ⇒ Object
- .encode_key(key) ⇒ Object
- .normalize_body(body) ⇒ Object
- .parse_iso8601(string) ⇒ Object
Class Method Details
.body_digest(body) ⇒ Object
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 |
# File 'lib/s3/helpers.rb', line 67 def body_digest( body ) case body when nil, '' Digest::SHA256.hexdigest( '' ) when String Digest::SHA256.hexdigest( body ) when StringIO content = body.read body.rewind Digest::SHA256.hexdigest( content ) when IO, File digest = Digest::SHA256.new while ( chunk = body.read( 1024 * 1024 ) ) digest.update( chunk ) end body.rewind digest.hexdigest else if body.respond_to?( :read ) content = body.read body.rewind if body.respond_to?( :rewind ) Digest::SHA256.hexdigest( content ) else Digest::SHA256.hexdigest( body.to_s ) end end end |
.build_query_string(params) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/s3/helpers.rb', line 12 def build_query_string( params ) return nil if params.nil? || params.empty? params.compact.sort.map do | key, value | # AWS signature requires %20 for spaces, not + encoded_key = URI.encode_www_form_component( key.to_s ).gsub( '+', '%20' ) if value.nil? encoded_key else # always include = even for empty string values (required for AWS signature) encoded_value = URI.encode_www_form_component( value.to_s ).gsub( '+', '%20' ) "#{ encoded_key }=#{ encoded_value }" end end.join( '&' ) end |
.encode_key(key) ⇒ Object
7 8 9 10 |
# File 'lib/s3/helpers.rb', line 7 def encode_key( key ) # encode_www_form_component encodes spaces as '+', but S3 requires '%20' URI.encode_www_form_component( key ).gsub( '+', '%20' ).gsub( '%2F', '/' ) end |
.normalize_body(body) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/s3/helpers.rb', line 35 def normalize_body( body ) case body when nil [ '', 0 ] when String [ body, body.bytesize ] when StringIO content = body.read body.rewind [ content, content.bytesize ] when IO, File if body.respond_to?( :size ) [ body, body.size ] elsif body.respond_to?( :stat ) [ body, body.stat.size ] else content = body.read body.rewind if body.respond_to?( :rewind ) [ content, content.bytesize ] end else if body.respond_to?( :read ) content = body.read body.rewind if body.respond_to?( :rewind ) [ content, content.bytesize ] else content = body.to_s [ content, content.bytesize ] end end end |
.parse_iso8601(string) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/s3/helpers.rb', line 28 def parse_iso8601( string ) return nil if string.nil? || string.empty? Time.parse( string ) rescue ArgumentError nil end |