Class: Miasma::Models::Storage::Aws
- Inherits:
-
Miasma::Models::Storage
- Object
- Miasma::Models::Storage
- Miasma::Models::Storage::Aws
- Defined in:
- lib/miasma/contrib/aws/storage.rb
Constant Summary collapse
- API_SERVICE =
Service name of the API
's3'- EUCA_API_SERVICE =
Service name of the API in eucalyptus
'objectstorage'- API_VERSION =
Supported version of the Storage API
'2006-03-01'
Instance Method Summary collapse
-
#all_result_pages(next_token, *result_key) {|options| ... } ⇒ Array
Fetch all results when tokens are being used for paging results.
-
#bucket_all ⇒ Array<Models::Storage::Bucket>
Return all buckets.
-
#bucket_destroy(bucket) ⇒ TrueClass, FalseClass
Destroy bucket.
-
#bucket_endpoint(bucket) ⇒ String
Custom bucket endpoint.
-
#bucket_reload(bucket) ⇒ Models::Storage::Bucket
Reload the bucket.
-
#bucket_save(bucket) ⇒ Models::Storage::Bucket
Save bucket.
-
#file_all(bucket) ⇒ Array<File>
Return all files within bucket.
-
#file_body(file) ⇒ IO, HTTP::Response::Body
Fetch the contents of the file.
-
#file_destroy(file) ⇒ TrueClass, FalseClass
Destroy file.
-
#file_filter(bucket, args) ⇒ Array<Models::Storage::File>
Return filtered files.
-
#file_path(file) ⇒ String
Escaped file path.
-
#file_reload(file) ⇒ Models::Storage::File
Reload the file.
-
#file_save(file) ⇒ Models::Storage::File
Save file.
-
#file_url(file, timeout_secs) ⇒ String
Create publicly accessible URL.
-
#initialize(args) ⇒ Aws
constructor
Simple init override to force HOST and adjust region for signatures if required.
-
#update_request(con, opts) ⇒ TrueClass
Simple callback to allow request option adjustments prior to signature calculation.
Methods included from Contrib::AwsApiCore::ApiCommon
#after_setup, #api_for, #connect, #connection, #custom_setup, #endpoint, included, #load_aws_file, #load_instance_credentials!, #make_request, #perform_request_retry, #retryable_allowed?, #sts_assume_role!, #sts_update_required?, #uri_escape
Constructor Details
#initialize(args) ⇒ Aws
Simple init override to force HOST and adjust region for signatures if required
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/miasma/contrib/aws/storage.rb', line 50 def initialize(args) args = args.to_smash cache_region = args[:aws_region] args[:aws_region] = args.fetch(:aws_bucket_region, 'us-east-1') super(args) aws_region = cache_region if(aws_bucket_region && aws_bucket_region != 'us-east-1') self.aws_host = "s3-#{aws_bucket_region}.amazonaws.com" else self.aws_host = 's3.amazonaws.com' end end |
Instance Method Details
#all_result_pages(next_token, *result_key) {|options| ... } ⇒ Array
this is customized to S3 since its API is slightly different than the usual token based fetching
Fetch all results when tokens are being used for paging results
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/miasma/contrib/aws/storage.rb', line 30 def all_result_pages(next_token, *result_key, &block) list = [] = next_token ? Smash.new('marker' => next_token) : Smash.new result = block.call() content = result.get(*result_key.dup) if(content.is_a?(Array)) list += content else list << content end set = result.get(*result_key.slice(0, 2)) if(set.is_a?(Hash) && set['IsTruncated'] && set['Contents']) content_key = (set['Contents'].respond_to?(:last) ? set['Contents'].last : set['Contents'])['Key'] list += all_result_pages(content_key, *result_key, &block) end list.compact end |
#bucket_all ⇒ Array<Models::Storage::Bucket>
Return all buckets
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/miasma/contrib/aws/storage.rb', line 149 def bucket_all result = all_result_pages(nil, :body, 'ListAllMyBucketsResult', 'Buckets', 'Bucket') do || request( :path => '/', :params => ) end result.map do |bkt| Bucket.new( self, :id => bkt['Name'], :name => bkt['Name'], :created => bkt['CreationDate'] ).valid_state end end |
#bucket_destroy(bucket) ⇒ TrueClass, FalseClass
Destroy bucket
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/miasma/contrib/aws/storage.rb', line 99 def bucket_destroy(bucket) if(bucket.persisted?) request( :path => '/', :method => :delete, :endpoint => bucket_endpoint(bucket), :expects => 204 ) true else false end end |
#bucket_endpoint(bucket) ⇒ String
properly escape bucket name
Custom bucket endpoint
142 143 144 |
# File 'lib/miasma/contrib/aws/storage.rb', line 142 def bucket_endpoint(bucket) ::File.join(endpoint, bucket.name) end |
#bucket_reload(bucket) ⇒ Models::Storage::Bucket
Reload the bucket
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/miasma/contrib/aws/storage.rb', line 117 def bucket_reload(bucket) if(bucket.persisted?) begin result = request( :path => '/', :method => :head, :endpoint => bucket_endpoint(bucket) ) rescue Error::ApiError::RequestError => e if(e.response.status == 404) bucket.data.clear bucket.dirty.clear else raise end end end bucket end |
#bucket_save(bucket) ⇒ Models::Storage::Bucket
Save bucket
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/miasma/contrib/aws/storage.rb', line 67 def bucket_save(bucket) unless(bucket.persisted?) req_args = Smash.new( :method => :put, :path => '/', :endpoint => bucket_endpoint(bucket) ) if(aws_bucket_region) req_args[:body] = XmlSimple.xml_out( Smash.new( 'CreateBucketConfiguration' => { 'LocationConstraint' => aws_bucket_region } ), 'AttrPrefix' => true, 'KeepRoot' => true ) req_args[:headers] = Smash.new( 'Content-Length' => req_args[:body].size.to_s ) end request(req_args) bucket.id = bucket.name bucket.valid_state end bucket end |
#file_all(bucket) ⇒ Array<File>
Return all files within bucket
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/miasma/contrib/aws/storage.rb', line 197 def file_all(bucket) result = all_result_pages(nil, :body, 'ListBucketResult', 'Contents') do || request( :path => '/', :params => , :endpoint => bucket_endpoint(bucket) ) end result.map do |file| File.new( bucket, :id => ::File.join(bucket.name, file['Key']), :name => file['Key'], :updated => file['LastModified'], :size => file['Size'].to_i ).valid_state end end |
#file_body(file) ⇒ IO, HTTP::Response::Body
Fetch the contents of the file
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/miasma/contrib/aws/storage.rb', line 405 def file_body(file) file_content = nil if(file.persisted?) result = request( :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :disable_body_extraction => true ) content = result[:body] begin if(content.is_a?(String)) file_content = StringIO.new(content) else if(content.respond_to?(:stream!)) content.stream! end file_content = content end rescue HTTP::StateError file_content = StringIO.new(content.to_s) end else file_content = StringIO.new('') end File::Streamable.new(file_content) end |
#file_destroy(file) ⇒ TrueClass, FalseClass
Destroy file
341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/miasma/contrib/aws/storage.rb', line 341 def file_destroy(file) if(file.persisted?) request( :method => :delete, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :expects => 204 ) true else false end end |
#file_filter(bucket, args) ⇒ Array<Models::Storage::File>
Return filtered files
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/miasma/contrib/aws/storage.rb', line 170 def file_filter(bucket, args) if(args[:prefix]) result = request( :path => '/', :endpoint => bucket_endpoint(bucket), :params => Smash.new( :prefix => args[:prefix] ) ) [result.get(:body, 'ListBucketResult', 'Contents')].flatten.compact.map do |file| File.new( bucket, :id => ::File.join(bucket.name, file['Key']), :name => file['Key'], :updated => file['LastModified'], :size => file['Size'].to_i ).valid_state end else bucket_all end end |
#file_path(file) ⇒ String
Returns escaped file path.
447 448 449 450 451 |
# File 'lib/miasma/contrib/aws/storage.rb', line 447 def file_path(file) file.name.split('/').map do |part| uri_escape(part) end.join('/') end |
#file_reload(file) ⇒ Models::Storage::File
Reload the file
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/miasma/contrib/aws/storage.rb', line 359 def file_reload(file) if(file.persisted?) name = file.name result = request( :path => file_path(file), :endpoint => bucket_endpoint(file.bucket) ) file.data.clear && file.dirty.clear info = result[:headers] file.load_data( :id => ::File.join(file.bucket.name, name), :name => name, :updated => info[:last_modified], :etag => info[:etag], :size => info[:content_length].to_i, :content_type => info[:content_type] ).valid_state end file end |
#file_save(file) ⇒ Models::Storage::File
Save file
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/miasma/contrib/aws/storage.rb', line 220 def file_save(file) if(file.dirty?) file.load_data(file.attributes) args = Smash.new headers = Smash[ Smash.new( :content_type => 'Content-Type', :content_disposition => 'Content-Disposition', :content_encoding => 'Content-Encoding' ).map do |attr, key| if(file.attributes[attr]) [key, file.attributes[attr]] end end.compact ] unless(headers.empty?) args[:headers] = headers end if(file.attributes[:body].respond_to?(:read) && file.attributes[:body].size >= Storage::MAX_BODY_SIZE_FOR_STRINGIFY) upload_id = request( args.merge( Smash.new( :method => :post, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :params => { :uploads => true } ) ) ).get(:body, 'InitiateMultipartUploadResult', 'UploadId') begin count = 1 parts = [] file.body.rewind while(content = file.body.read(Storage::READ_BODY_CHUNK_SIZE * 1.5)) parts << [ count, request( :method => :put, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :headers => Smash.new( 'Content-Length' => content.size, 'Content-MD5' => Digest::MD5.base64digest(content) ), :params => Smash.new( 'partNumber' => count, 'uploadId' => upload_id ), :body => content ).get(:headers, :etag) ] count += 1 end complete = XmlSimple.xml_out( Smash.new( 'CompleteMultipartUpload' => { 'Part' => parts.map{|part| {'PartNumber' => part.first, 'ETag' => part.last} } } ), 'AttrPrefix' => true, 'KeepRoot' => true ) result = request( :method => :post, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :params => Smash.new( 'uploadId' => upload_id ), :headers => Smash.new( 'Content-Length' => complete.size ), :body => complete ) file.etag = result.get(:body, 'CompleteMultipartUploadResult', 'ETag') rescue => e request( :method => :delete, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket), :params => { 'uploadId' => upload_id }, :expects => 204 ) raise end else if(file.attributes[:body].respond_to?(:readpartial)) args.set(:headers, 'Content-Length', file.body.size.to_s) file.body.rewind args[:body] = file.body.readpartial(file.body.size) file.body.rewind else args.set(:headers, 'Content-Length', 0) end result = request( args.merge( Smash.new( :method => :put, :path => file_path(file), :endpoint => bucket_endpoint(file.bucket) ) ) ) file.etag = result.get(:headers, :etag) end file.id = ::File.join(file.bucket.name, file.name) file.valid_state end file end |
#file_url(file, timeout_secs) ⇒ String
Create publicly accessible URL
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/miasma/contrib/aws/storage.rb', line 384 def file_url(file, timeout_secs) if(file.persisted?) signer.generate_url( :get, ::File.join(uri_escape(file.bucket.name), file_path(file)), :headers => Smash.new( 'Host' => aws_host ), :params => Smash.new( 'X-Amz-Date' => Contrib::AwsApiCore.time_iso8601, 'X-Amz-Expires' => timeout_secs ) ) else raise Error::ModelPersistError.new "#{file} has not been saved!" end end |
#update_request(con, opts) ⇒ TrueClass
this only updates when :body is defined. if a :post is
Simple callback to allow request option adjustments prior to signature calculation
happening (which implicitly forces :form) or :json is used it will not properly checksum. (but that’s probably okay)
440 441 442 443 444 |
# File 'lib/miasma/contrib/aws/storage.rb', line 440 def update_request(con, opts) opts[:headers]['x-amz-content-sha256'] = Digest::SHA256. hexdigest(opts.fetch(:body, '')) true end |