Class: S3Light::Object

Inherits:
Struct
  • Object
show all
Defined in:
lib/s3-light/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, bucket, id, input, persisted = false) ⇒ Object

Returns a new instance of Object.



5
6
7
# File 'lib/s3-light/object.rb', line 5

def initialize(client, bucket, id, input, persisted = false)
  super(client, bucket, id, input, persisted)
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket

Returns:

  • (Object)

    the current value of bucket



4
5
6
# File 'lib/s3-light/object.rb', line 4

def bucket
  @bucket
end

#clientObject

Returns the value of attribute client

Returns:

  • (Object)

    the current value of client



4
5
6
# File 'lib/s3-light/object.rb', line 4

def client
  @client
end

#inputObject

Returns the value of attribute input

Returns:

  • (Object)

    the current value of input



4
5
6
# File 'lib/s3-light/object.rb', line 4

def input
  @input
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



4
5
6
# File 'lib/s3-light/object.rb', line 4

def key
  @key
end

#persistedObject

Returns the value of attribute persisted

Returns:

  • (Object)

    the current value of persisted



4
5
6
# File 'lib/s3-light/object.rb', line 4

def persisted
  @persisted
end

Instance Method Details

#__destroy!(connection) ⇒ Object



104
105
106
# File 'lib/s3-light/object.rb', line 104

def __destroy!(connection)
  connection.make_request(:delete, "/#{bucket.name}/#{key}")
end

#__download(to, connection) ⇒ Object



108
109
110
# File 'lib/s3-light/object.rb', line 108

def __download(to, connection)
  connection.download_file("/#{bucket.name}/#{key}", to)
end

#__save!(connection) ⇒ Object



100
101
102
# File 'lib/s3-light/object.rb', line 100

def __save!(connection)
  connection.make_request(:put, "/#{bucket.name}/#{key}", body: input)
end

#aclObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/s3-light/object.rb', line 43

def acl
  return @acl if defined?(@acl)

  response = client.with_connection do |connection|
    connection.make_request(:get, "/#{bucket.name}/#{key}?acl=1")
  end

  @acl = response.xml.remove_namespaces!.xpath('//AccessControlList/Grant').each_with_object({}) do |grant, result|
    permission = grant.at_xpath('Permission').text
    grantee = grant.at_xpath('Grantee')

    grantee_type = grantee['type']

    identifier =
      case grantee_type
      when 'CanonicalUser'
        grantee.at_xpath('ID')&.text || 'CanonicalUser'
      when 'Group'
        grantee.at_xpath('URI')&.text
      else
        grantee.at_xpath('EmailAddress')&.text
      end

    result[identifier] = permission if identifier
  end
end

#acl=(new_acl) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/s3-light/object.rb', line 71

def acl=(new_acl)
  xml_body = build_acl_xml(new_acl)

  client.with_connection do |connection|
    connection.make_request(:put, "/#{bucket.name}/#{key}?acl=1", body: xml_body)
  end

  @acl = new_acl
end

#destroy!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/s3-light/object.rb', line 28

def destroy!
  unless persisted
    raise S3Light::Error, 'Object does not exist'
  end

  self.client.with_connection do |connection|
    __destroy!(connection)
  rescue S3Light::Connection::HttpError => e
    raise e unless e.code == 404
  end

  self.persisted = false
  true
end

#download(to: nil) ⇒ Object

Raises:



81
82
83
84
85
86
87
88
89
# File 'lib/s3-light/object.rb', line 81

def download(to: nil)
  raise S3Light::Error, 'Object does not exist' unless persisted

  to ||= "/tmp/#{SecureRandom.hex}-#{key}"

  self.client.with_connection do |connection|
    __download(to, connection)
  end
end

#inspectObject



13
14
15
# File 'lib/s3-light/object.rb', line 13

def inspect
  "#<#{self.class.name} @id=#{key} @bucket=#{bucket.name}>"
end

#open(mode = 'r') {|file| ... } ⇒ Object

Yields:

  • (file)

Raises:



91
92
93
94
95
96
97
98
# File 'lib/s3-light/object.rb', line 91

def open(mode = 'r', &block)
  raise S3Light::Error, 'Object does not exist' unless persisted
  download_path = download
  file = File.open(download_path, mode)
  yield file
  self.input = file
  file
end

#persisted?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/s3-light/object.rb', line 9

def persisted?
  persisted
end

#save!Object

Raises:



17
18
19
20
21
22
23
24
25
26
# File 'lib/s3-light/object.rb', line 17

def save!
  raise S3Light::Error, 'Input is empty' if input.nil?

  self.client.with_connection do |connection|
    __save!(connection)
  end
  self.persisted = true

  self
end