Class: AWS::S3::S3Object
- Inherits:
-
Object
- Object
- AWS::S3::S3Object
- Defined in:
- lib/mock/aws/s3/object.rb
Constant Summary collapse
- TEMP_PATH =
begin if defined?(::Rails) Rails.root.to_s else '.' end end
Class Method Summary collapse
-
.copy(key, copy_key, bucket = nil, options = {}) ⇒ Object
Makes a copy of the object with
key
tocopy_key
, preserving the ACL of the existing object if the:copy_acl
option is true (default false). - .delete(key, bucket = nil, options = {}) ⇒ Object
-
.exists?(key, bucket = nil) ⇒ Boolean
# Fetch information about the object with
key
frombucket
. - .find(key, bucket = nil) ⇒ Object
-
.path!(bucket, name, options = {}) ⇒ Object
:nodoc:.
- .store(key, data, bucket = nil, options = {}) ⇒ Object (also: create, save)
- .url_for(key, bucket = nil, options = {}) ⇒ Object
- .value(key, bucket = nil, options = {}, &block) ⇒ Object
Class Method Details
.copy(key, copy_key, bucket = nil, options = {}) ⇒ Object
Makes a copy of the object with key
to copy_key
, preserving the ACL of the existing object if the :copy_acl
option is true (default false).
37 38 39 40 41 42 43 |
# File 'lib/mock/aws/s3/object.rb', line 37 def copy(key, copy_key, bucket = nil, = {}) bucket = bucket_name(bucket) source_key = path!(bucket, key) target_key = path!(bucket, copy_key) FileUtils.makedirs File.dirname(target_key) FileUtils.cp_r source_key, target_key end |
.delete(key, bucket = nil, options = {}) ⇒ Object
61 62 63 |
# File 'lib/mock/aws/s3/object.rb', line 61 def delete(key, bucket = nil, = {}) File.unlink path!(bucket, key, ) end |
.exists?(key, bucket = nil) ⇒ Boolean
# Fetch information about the object with key
from bucket
. Information includes content type, content length, # last modified time, and others. # # If the specified key does not exist, NoSuchKey is raised. def about(key, bucket = nil, options = {})
response = head(path!(bucket, key, ), )
raise NoSuchKey.new("No such key `#{key}'", bucket) if response.code == 404
About.new(response.headers)
end
57 58 59 |
# File 'lib/mock/aws/s3/object.rb', line 57 def exists?(key, bucket = nil) File.exists?(path!(bucket, key)) end |
.find(key, bucket = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/mock/aws/s3/object.rb', line 19 def find(key, bucket = nil) raise NoSuchKey.new("No such key `#{key}'", bucket) unless exists?(key, bucket) bucket = Bucket.new(:name => bucket) object = bucket.new_object object.key = key object end |
.path!(bucket, name, options = {}) ⇒ Object
:nodoc:
87 88 89 90 91 92 93 94 |
# File 'lib/mock/aws/s3/object.rb', line 87 def path!(bucket, name, = {}) #:nodoc: # We're using the second argument for options if bucket.is_a?(Hash) .replace(bucket) bucket = nil end TEMP_PATH.clone << File.join('/tmp', 'mock-aws-s3', bucket_name(bucket), name) end |
.store(key, data, bucket = nil, options = {}) ⇒ Object Also known as: create, save
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mock/aws/s3/object.rb', line 65 def store(key, data, bucket = nil, = {}) validate_key!(key) # Must build path before infering content type in case bucket is being used for options path = path!(bucket, key, ) infer_content_type!(key, ) FileUtils.makedirs File.dirname(path) File.open(path, 'wb') do |f| f.write data end end |
.url_for(key, bucket = nil, options = {}) ⇒ Object
15 16 17 |
# File 'lib/mock/aws/s3/object.rb', line 15 def url_for(key, bucket = nil, ={}) "file://#{File.(path!(bucket, key))}" end |
.value(key, bucket = nil, options = {}, &block) ⇒ Object
10 11 12 13 |
# File 'lib/mock/aws/s3/object.rb', line 10 def value(key, bucket = nil, = {}, &block) data = File.open(path!(bucket, key, )) {|f| f.read} Value.new OpenStruct.new(:body=>data) end |