s33r 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ # TODO: parser to provide a tree representation of keys in a bucket
@@ -0,0 +1,28 @@
1
+ module S3
2
+ # a client for dealing with a single bucket
3
+ class NamedBucket < Client
4
+ attr_accessor :bucket_name
5
+
6
+ # TODO: check bucket exists before setting it
7
+ # options available:
8
+ # :public_contents => true: all items put into bucket are made public
9
+ def initialize(aws_access_key, aws_secret_access_key, bucket_name, options={}, &block)
10
+ super(aws_access_key, aws_secret_access_key)
11
+ @bucket_name = bucket_name
12
+ @client_headers.merge!(canned_acl_header('public-read')) if options[:public_contents]
13
+ yield self if block_given?
14
+ end
15
+
16
+ def root
17
+ list_bucket(@bucket_name)
18
+ end
19
+
20
+ def put_text(string, resource_key, headers={})
21
+ super(string, @bucket_name, resource_key, headers)
22
+ end
23
+
24
+ def put_file(filename, resource_key=nil, headers={})
25
+ super(filename, @bucket_name, resource_key, headers)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'net/http'
2
+
3
+ class Net::HTTPResponse
4
+ attr_accessor :success
5
+
6
+ def ok?
7
+ success
8
+ end
9
+
10
+ def success
11
+ response.is_a?(Net::HTTPOK)
12
+ end
13
+
14
+ def to_s
15
+ body
16
+ end
17
+ end
18
+
19
+ class Net::HTTPGenericRequest
20
+ attr_accessor :chunk_size
21
+
22
+ private
23
+ def send_request_with_body_stream(sock, ver, path, f)
24
+ raise ArgumentError, "Content-Length not given and Transfer-Encoding is not `chunked'" unless content_length() or chunked?
25
+ unless content_type()
26
+ warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE
27
+ set_content_type 'application/x-www-form-urlencoded'
28
+ end
29
+ @chunk_size ||= 1024
30
+ write_header sock, ver, path
31
+ if chunked?
32
+ while s = f.read(@chunk_size)
33
+ sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
34
+ end
35
+ sock.write "0\r\n\r\n"
36
+ else
37
+ while s = f.read(@chunk_size)
38
+ sock.write s
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module S3
2
+ module S3Exception
3
+
4
+ class MethodNotAvailable < Exception
5
+ end
6
+
7
+ class MissingRequiredHeaders < Exception
8
+ end
9
+
10
+ class UnsupportedCannedACL < Exception
11
+ end
12
+
13
+ class UnsupportedHTTPMethod < Exception
14
+ end
15
+
16
+ class MalformedBucketName < Exception
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,87 @@
1
+ require 'rubygems'
2
+ require_gem 'rspec'
3
+
4
+ require File.dirname(__FILE__) + '/../../lib/s33r.rb'
5
+ include S3
6
+
7
+ context 'S3 module' do
8
+
9
+ setup do
10
+ @for_request_method = 'PUT'
11
+ @for_request_path = "/quotes/nelson"
12
+ @for_request_headers = {
13
+ "Content-Md5" => "c8fdb181845a4ca6b8fec737b3581d76",
14
+ "Content-Type" => "text/html",
15
+ "Date" => "Thu, 17 Nov 2005 18:49:58 GMT",
16
+ "X-Amz-Meta-Author" => "foo@bar.com",
17
+ "X-Amz-Magic" => "abracadabra"
18
+ }
19
+ @with_access_key = '44CF9590006BF252F707'
20
+ @with_secret_access_key = 'OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV'
21
+
22
+ @for_incomplete_headers = @for_request_headers.clone.delete_if do |key,value|
23
+ 'Content-Type' == key or 'Date' == key
24
+ end
25
+
26
+ @correct_canonical_string = "PUT\nc8fdb181845a4ca6b8fec737b3581d76\n" +
27
+ "text/html\nThu, 17 Nov 2005 18:49:58 GMT\nx-amz-magic:abracadabra\n" +
28
+ "x-amz-meta-author:foo@bar.com\n/quotes/nelson"
29
+ @correct_signature = "jZNOcbfWmD/A/f3hSvVzXZjM2HU="
30
+ @correct_auth_header = "AWS #{@with_access_key}:#{@correct_signature}"
31
+ end
32
+
33
+ specify 'should generate correct canonical strings' do
34
+ generate_canonical_string(@for_request_method, @for_request_path,
35
+ @for_request_headers).should.equal @correct_canonical_string
36
+ end
37
+
38
+ specify 'should generate correct signatures' do
39
+ generate_signature(@with_secret_access_key,
40
+ @correct_canonical_string).should.equal @correct_signature
41
+ end
42
+
43
+ specify 'should generate correct auth headers' do
44
+ generate_auth_header_value(@for_request_method, @for_request_path, @for_request_headers,
45
+ @with_access_key, @with_secret_access_key).should.equal @correct_auth_header
46
+ end
47
+
48
+ specify 'should not generate auth header if bad HTTP method passed' do
49
+ lambda { generate_auth_header_value('duff', nil, nil, nil, nil) }.should.raise
50
+ S3Exception::MethodNotAvailable
51
+ end
52
+
53
+ specify 'should not generate auth header if required headers missing' do
54
+ lambda { generate_auth_header_value('PUT', '/', @for_incomplete_headers,
55
+ nil, nil) }.should.raise S3Exception::MissingRequiredHeaders
56
+ end
57
+
58
+ specify 'when generating auth header, should allow addition of Date and Content-Type headers' do
59
+ now = Time.now
60
+
61
+ fixed_headers = add_default_headers(@for_incomplete_headers, :date => now,
62
+ :content_type => 'text/html')
63
+
64
+ fixed_headers['Date'].should.equal now.httpdate
65
+ fixed_headers['Content-Type'].should.equal 'text/html'
66
+ end
67
+
68
+ specify 'should not generate canned ACL header if invalid canned ACL supplied' do
69
+ lambda { canned_acl_header('duff') }.should.raise
70
+ S3Exception::UnsupportedCannedACL
71
+ end
72
+
73
+ specify 'should correctly add canned ACL headers' do
74
+ new_headers = canned_acl_header('private', { 'Content-Type' => 'text/html' })
75
+ new_headers.should.have(2).keys
76
+ new_headers.keys.should.include 'Content-Type'
77
+ new_headers.keys.should.include 'x-amz-acl'
78
+ new_headers['x-amz-acl'].should.equal 'private'
79
+ end
80
+
81
+ specify 'should set sensible defaults for missing Content-Type and Date headers' do
82
+ fixed_headers = add_default_headers(@for_incomplete_headers)
83
+ fixed_headers['Content-Type'].should.equal ''
84
+ fixed_headers.include?('Date').should.not.be nil
85
+ end
86
+
87
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: s33r
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2006-07-25 00:00:00 +01:00
8
+ summary: A library for accessing Amazon S3
9
+ require_paths:
10
+ - lib
11
+ email: elliot@moochlabs.com/
12
+ homepage: http://moochlabs.com/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: s33r
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Elliot Smith
30
+ files:
31
+ - bin/s3cli.rb
32
+ - bin/config.yml
33
+ - lib/s33r
34
+ - lib/s33r.rb
35
+ - lib/s33r/external
36
+ - lib/s33r/client.rb
37
+ - lib/s33r/list_bucket_result.rb
38
+ - lib/s33r/net_http_overrides.rb
39
+ - lib/s33r/s3_exception.rb
40
+ - lib/s33r/core.rb
41
+ - lib/s33r/named_bucket.rb
42
+ - lib/s33r/external/mimetypes.rb
43
+ - test/spec
44
+ - test/spec/spec_core.rb
45
+ - LICENSE.txt
46
+ - README.txt
47
+ - MIT-LICENSE
48
+ test_files:
49
+ - test/spec/spec_core.rb
50
+ rdoc_options: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies: []
61
+