Class: AWS::S3::S3Object

Inherits:
Object
  • Object
show all
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

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, options = {})
  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, options = {})
  File.unlink path!(bucket, key, options)
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, options), options)
raise NoSuchKey.new("No such key `#{key}'", bucket) if response.code == 404
About.new(response.headers)

end

Returns:

  • (Boolean)


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

Raises:

  • (NoSuchKey)


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, options = {}) #:nodoc:
  # We're using the second argument for options
  if bucket.is_a?(Hash)
    options.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, options = {})
  validate_key!(key)
  # Must build path before infering content type in case bucket is being used for options
  path = path!(bucket, key, options)
  infer_content_type!(key, options)

  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, options={})
      "file://#{File.expand_path(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, options = {}, &block)
  data = File.open(path!(bucket, key, options)) {|f| f.read}
  Value.new OpenStruct.new(:body=>data)
end