Class: SwiftStorage::Object

Inherits:
Node
  • Object
show all
Defined in:
lib/swift_storage/object.rb

Constant Summary

Constants included from Headers

Headers::ACCOUNT_TEMP_URL_KEY, Headers::AUTH_KEY, Headers::AUTH_TOKEN, Headers::AUTH_USER, Headers::CACHE_CONTROL, Headers::CONNECTION, Headers::CONTAINER_READ, Headers::CONTAINER_WRITE, Headers::CONTENT_DISPOSITION, Headers::CONTENT_TYPE, Headers::DELETE_AFTER, Headers::DELETE_AT, Headers::DESTINATION, Headers::EXPIRES, Headers::OBJECT_MANIFEST, Headers::PROXY_CONNECTION, Headers::STORAGE_TOKEN, Headers::STORAGE_URL

Instance Attribute Summary collapse

Attributes inherited from Node

#name, #parent

Instance Method Summary collapse

Methods inherited from Node

#clear_cache, #delete, #delete_if_exists, #exists?, #get_lines, #headers, #initialize, #metadata, #request, #service, #to_s

Methods included from Utils

#hmac, #sig_to_hex, #struct

Constructor Details

This class inherits a constructor from SwiftStorage::Node

Instance Attribute Details

#cache_controlString

Object cache control header.

Returns:

  • (String)

    the current value of cache_control



15
16
17
# File 'lib/swift_storage/object.rb', line 15

def cache_control
  @cache_control
end

#content_lengthString

Content length of the Object, in bytes.

Returns:

  • (String)

    the current value of content_length



15
16
17
# File 'lib/swift_storage/object.rb', line 15

def content_length
  @content_length
end

#content_typeString

Content type of the Object, eg: image/png.

Returns:

  • (String)

    the current value of content_type



15
16
17
# File 'lib/swift_storage/object.rb', line 15

def content_type
  @content_type
end

#expiresString

When the object is set to expire.

Returns:

  • (String)

    the current value of expires



15
16
17
# File 'lib/swift_storage/object.rb', line 15

def expires
  @expires
end

Instance Method Details

#copy_from(source, optional_headers = {}) ⇒ SwiftStorage::Object

Creates a copy of an object that is already stored in Swift.

Parameters:

  • source (String, SwiftStorage::Object)

    The container and object name of the source object or the SwiftStorage::Object source

  • optional_headers (Hash) (defaults to: {})

    All optional headers supported by Swift API for object copy

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/swift_storage/object.rb', line 164

def copy_from(source, optional_headers = {})
  case source
  when SwiftStorage::Object
    path = source.relative_path
  when String
    path = source
  else
    raise ArgumentError.new('Invalid source type')
  end

  request(path + '?multipart-manifest=get', method: :copy, headers: optional_headers.merge(DESTINATION => relative_path))
  self
end

#read(output_stream = nil) ⇒ String, output_stream

Read the object data

This will always make a request to the API server and will not use cache

Parameters:

  • output_stream (IO) (defaults to: nil)

    An optional stream to write the object's content to. This can be a File or and IO object. This method will NEVER rewind output_stream, either before writing and after.

Returns:

  • (String, output_stream)

    If output_stream is nil or ommited, it returns a string with the object content. If output_stream is given, returns it.



34
35
36
37
38
39
40
41
# File 'lib/swift_storage/object.rb', line 34

def read(output_stream=nil)
  response = request(relative_path, :method => :get, :output_stream => output_stream)
  if output_stream
    output_stream
  else
    response.body
  end
end

#relative_pathString

Returns the object's relative path (container name with object name)

Returns:

  • (String)

    The object relative path.



213
214
215
# File 'lib/swift_storage/object.rb', line 213

def relative_path
  File.join(container.name, name)
end

#stream_to_file(output_path) ⇒ output_path

Stream the object data to a file

This will always make a request to the API server and will not use cache

Parameters:

  • output_path (String)

    The path to the output file.

Returns:

  • (output_path)

    The passed path.



53
54
55
56
57
58
# File 'lib/swift_storage/object.rb', line 53

def stream_to_file(output_path)
  open(output_path, 'wb') do |io|
    read(io)
  end
  output_path
end

#temp_url(expires = Time.now + 3600, method: :get) ⇒ String

Generates a public URL with an expiration time

Parameters:

  • expires (Time) (defaults to: Time.now + 3600)

    The absolute time when the URL will expire.

  • method (Symbol) (defaults to: :get)

    The HTTP method to allow, can be :get, :put, :head.

Returns:

  • (String)

    A temporary URL to the object.



190
191
192
193
# File 'lib/swift_storage/object.rb', line 190

def temp_url(expires = nil, ssl: true, method: :get, params: {})
  expires ||= Time.now + 3600
  service.create_temp_url(container.name, name, expires, method, ssl, params)
end

#urlString

Note:

This URL is unsigneds and the container authorization will apply. If the container do not allow public access, this URL will require an authentication token.

Returns the object's URL

Returns:

  • (String)

    The object URL.



204
205
206
# File 'lib/swift_storage/object.rb', line 204

def url
  File.join(service.storage_url, relative_path)
end

#write(input_stream = nil, content_type: nil, attachment: false, delete_at: nil, delete_after: nil, cache_control: nil, expires: nil, object_manifest: nil, part_location: nil, metadata: nil) ⇒ input_stream

Note:

If you want to only update the metadata, you may omit input_stream but you must specify all other options otherwise they will be overwritten.

Note:

Some headers specified here may not work with a specific swift server as they must be enabled in the server configuration.

Write the object

This will always make a request to the API server and will not use cache

Parameters:

  • input_stream (String, IO) (defaults to: nil)

    The data to upload, if ommited, the write will not override the body and instead it will update the metadata and other options. If input_stream is an IO object, it must be seeked to the proper position, this method will NEVER seek or rewind the stream.

  • content_type (String) (defaults to: nil)

    The content type, eg: image/png.

  • attachment (Boolean) (defaults to: false)

    If true the file will be served with Content-Disposition: attachment.

  • delete_at (Time) (defaults to: nil)

    If set, the server will delete the object at the specified time.

  • delete_after (Time) (defaults to: nil)

    If set, the server will delete the object after the specified time.

  • cache_control (String) (defaults to: nil)

    The value for the 'Cache-Control' header when serving the object. The value is not parsed and served unmodified as is. If you set max-age, it will always be served with the same max-age value. To have the resource expire at point of time, use the expires header.

  • expires (Symbol, Time) (defaults to: nil)

    Set the Expires header. Expires may also have the special value :never which override cache_control and set the expiration time in a long time.

  • object_manifest (String) (defaults to: nil)

    When set, this object acts as a large object manifest. The value should be <container>/<prefix> where <container> is the container the object segments are in and <prefix> is the common prefix for all the segments.

Returns:

  • (input_stream)

    Return the input_stream argument, or nil if input_stream is ommited.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/swift_storage/object.rb', line 107

def write(input_stream=nil,
          content_type: nil,
          attachment: false,
          delete_at: nil,
          delete_after: nil,
          cache_control: nil,
          expires: nil,
          object_manifest: nil,
          part_location: nil,
          metadata: nil)

  h = {}

  input_stream.nil? or content_type or raise ArgumentError, 'Content_type is required if input_stream is given'

  object_manifest.nil? or input_stream.nil? or raise ArgumentError, 'Input must be nil on object manifest'

  if expires == :never
    expires = Time.at(4_000_000_000)
    cache_control = "public, max_age=4000000000"
  end

  h[CONTENT_DISPOSITION] = attachment ? 'attachment' : 'inline'
  h[OBJECT_MANIFEST] = object_manifest if object_manifest
  h[CONTENT_TYPE] = content_type if content_type
  h[EXPIRES] = expires.httpdate if expires
  h[CACHE_CONTROL] = cache_control if cache_control

  if delete_at
    h[DELETE_AT] = delete_at.to_i.to_s
  elsif delete_after
    h[DELETE_AFTER] = delete_after.to_i.to_s
  end

  (h, )

  method =  input_stream || object_manifest ? :put : :post
  path = part_location || relative_path

  request(path,
          :method => method,
          :headers => h,
          :input_stream => input_stream)
  clear_cache
  input_stream
end