Class: EC2::Common::S3Support
- Inherits:
-
Object
- Object
- EC2::Common::S3Support
- Defined in:
- lib/ec2/common/s3support.rb
Constant Summary collapse
- DEFAULT_SIGV =
EC2::Common::SIGV4
- DEFAULT_REGION =
'us-east-1'
Instance Attribute Summary collapse
-
#pass ⇒ Object
Returns the value of attribute pass.
-
#s3_url ⇒ Object
Returns the value of attribute s3_url.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.bucket_name_s3_safe?(bucket_name) ⇒ Boolean
Return true if the bucket name is S3 safe.
-
.bucket_name_s3_v2_safe?(bucket_name) ⇒ Boolean
Return true if the bucket name is S3 (v2) safe.
Instance Method Summary collapse
- #check_bucket_exists(bucket, options = {}) ⇒ Object
- #copy(bucket, key, source, options = {}) ⇒ Object
- #create_bucket(bucket, location, options = {}) ⇒ Object
- #delete(bucket, key = "", options = {}) ⇒ Object
- #fix_s3_url(s3_url) ⇒ Object
- #get(bucket, key, path = nil, options = {}) ⇒ Object
- #get_acl(bucket, key, options = {}) ⇒ Object
- #get_bucket_location(bucket, options = {}) ⇒ Object
- #get_bucket_url(bucket) ⇒ Object
-
#initialize(s3_url, user, pass, format = nil, debug = nil, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ S3Support
constructor
A new instance of S3Support.
- #list_bucket(bucket, prefix = nil, max_keys = nil, path = nil, options = {}) ⇒ Object
- #put(bucket, key, file, options = {}) ⇒ Object
Constructor Details
#initialize(s3_url, user, pass, format = nil, debug = nil, sigv = DEFAULT_SIGV, region = DEFAULT_REGION) ⇒ S3Support
Returns a new instance of S3Support.
72 73 74 75 76 77 78 79 80 |
# File 'lib/ec2/common/s3support.rb', line 72 def initialize(s3_url, user, pass, format=nil, debug=nil, sigv=DEFAULT_SIGV, region=DEFAULT_REGION) @user = user @pass = pass @s3_url = fix_s3_url(s3_url) @format = format @debug = debug @sigv = sigv @region = region end |
Instance Attribute Details
#pass ⇒ Object
Returns the value of attribute pass.
23 24 25 |
# File 'lib/ec2/common/s3support.rb', line 23 def pass @pass end |
#s3_url ⇒ Object
Returns the value of attribute s3_url.
23 24 25 |
# File 'lib/ec2/common/s3support.rb', line 23 def s3_url @s3_url end |
#user ⇒ Object
Returns the value of attribute user.
23 24 25 |
# File 'lib/ec2/common/s3support.rb', line 23 def user @user end |
Class Method Details
.bucket_name_s3_safe?(bucket_name) ⇒ Boolean
Return true if the bucket name is S3 safe.
Per the S3 dev guide @ docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?BucketRestrictions.html
-
Be between 3 and 255 characters long
-
Start with a number or letter
-
Contain lowercase letters, numbers, periods (.), underscores (_), and dashes (-)
-
Not be in an IP address style (e.g., “192.168.5.4”)
-
Notes:
-
!!(.…) silliness to force a boolean to be returned
40 41 42 43 44 45 46 |
# File 'lib/ec2/common/s3support.rb', line 40 def self.bucket_name_s3_safe?(bucket_name) # Can most probably fold this all into 1 grand regexp but # for now opt for semi-clarity. !!((3..255).include?(bucket_name.length) and (/^[a-z0-9][a-z0-9\._-]+$/ =~ bucket_name) and (/^(\d{1,3}\.){3}\d{1,3}$/ !~ bucket_name)) end |
.bucket_name_s3_v2_safe?(bucket_name) ⇒ Boolean
Return true if the bucket name is S3 (v2) safe.
Per the S3 dev guide @ docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?BucketRestrictions.html
-
Bucket names should not contain underscores (_)
-
Bucket names should be between 3 and 63 characters long
-
Bucket names should not end with a dash
-
Bucket names cannot contain dashes next to periods (e.g., “my-.bucket.com” and “my.-bucket” are invalid)
-
Bucket names must only contain lower case letters
and naturally also fulfills bucket_name_s3_safe?() requirements
-
Notes:
-
!!(.…) silliness to force a boolean to be returned
62 63 64 65 66 67 68 69 70 |
# File 'lib/ec2/common/s3support.rb', line 62 def self.bucket_name_s3_v2_safe?(bucket_name) # Can most probably fold this all into 1 grand regexp but # for now opt for semi-clarity. !!(self.bucket_name_s3_safe?(bucket_name) and bucket_name.length <= 63 and not bucket_name.include?('_') and bucket_name[-1] != ?- and /(-\.)|(\.-)/ !~ bucket_name) end |
Instance Method Details
#check_bucket_exists(bucket, options = {}) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/ec2/common/s3support.rb', line 121 def check_bucket_exists(bucket, ={}) url, bkt = get_bucket_url(bucket) http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::head(url, bkt, , @user, @pass, @debug, signature, @region) end end |
#copy(bucket, key, source, options = {}) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/ec2/common/s3support.rb', line 174 def copy(bucket, key, source, ={}) url, bkt = get_bucket_url(bucket) url << "/#{CGI::escape(key)}" ['x-amz-copy-source'] = CGI::escape(source) http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::put(url, bkt, nil, , @user, @pass, @debug, signature, @region) end end |
#create_bucket(bucket, location, options = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ec2/common/s3support.rb', line 136 def create_bucket(bucket, location, ={}) url, bkt = get_bucket_url(bucket) binary_data = "" if (location != nil) binary_data = "<CreateBucketConstraint><LocationConstraint>#{location}</LocationConstraint></CreateBucketConstraint>" end http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::putdir(url, bkt, binary_data, , @user, @pass, @debug, signature, @region) end end |
#delete(bucket, key = "", options = {}) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/ec2/common/s3support.rb', line 183 def delete(bucket, key="", ={}) url, bkt = get_bucket_url(bucket) url << "/#{CGI::escape(key)}" http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::delete(url, bkt, , @user, @pass, @debug, signature, @region) end end |
#fix_s3_url(s3_url) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/ec2/common/s3support.rb', line 82 def fix_s3_url(s3_url) if s3_url !~ %r{://} s3_url = "https://#{s3_url}" end if s3_url[-1..-1] != "/" s3_url << "/" end s3_url end |
#get(bucket, key, path = nil, options = {}) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/ec2/common/s3support.rb', line 158 def get(bucket, key, path=nil, ={}) url, bkt = get_bucket_url(bucket) url << "/#{CGI::escape(key)}" http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::get(url, bkt, path, , @user, @pass, nil, nil, @debug, signature, @region) end end |
#get_acl(bucket, key, options = {}) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/ec2/common/s3support.rb', line 111 def get_acl(bucket, key, ={}) begin url, bkt = get_bucket_url(bucket) url << "/#{CGI::escape(key)}?acl" http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::get(url, bkt, nil, , @user, @pass, nil, nil, @debug, signature, @region) end end end |
#get_bucket_location(bucket, options = {}) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/ec2/common/s3support.rb', line 128 def get_bucket_location(bucket, ={}) url, bkt = get_bucket_url(bucket) url << "?location" http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::get(url, bkt, nil, , @user, @pass, nil, nil, @debug, signature, @region) end end |
#get_bucket_url(bucket) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ec2/common/s3support.rb', line 92 def get_bucket_url(bucket) # The returned variable should not end with '/', makes constructing the path for sigv4 easier if not @format if bucket.include? "." @format = :path else @format = :subdomain end end case @format when :subdomain uri = URI.parse(@s3_url) return ["#{uri.scheme}://#{bucket}.#{uri.host}", bucket] when :path return ["#{@s3_url}#{bucket}", bucket] end end |
#list_bucket(bucket, prefix = nil, max_keys = nil, path = nil, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ec2/common/s3support.rb', line 147 def list_bucket(bucket, prefix=nil, max_keys=nil, path=nil, ={}) url, bkt = get_bucket_url(bucket) params = [] params << "prefix=#{CGI::escape(prefix)}" if prefix params << "max-keys=#{CGI::escape(max_keys)}" if max_keys url << "?" + params.join("&") unless params.empty? http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::get(url, bkt, path, , @user, @pass, nil, nil, @debug, signature, @region) end end |
#put(bucket, key, file, options = {}) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/ec2/common/s3support.rb', line 166 def put(bucket, key, file, ={}) url, bkt = get_bucket_url(bucket) url << "/#{CGI::escape(key)}" http_fallback(url, @sigv) do |url, signature| EC2::Common::HTTP::put(url, bkt, file, , @user, @pass, @debug, signature, @region) end end |