Class: SwiftStorage::Object
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
-
#cache_control ⇒ String
Object cache control header.
-
#content_length ⇒ String
Content length of the Object, in bytes.
-
#content_type ⇒ String
Content type of the Object, eg:
image/png
. -
#expires ⇒ String
When the object is set to expire.
Attributes inherited from Node
Instance Method Summary collapse
-
#copy_from(source, optional_headers = {}) ⇒ SwiftStorage::Object
Creates a copy of an object that is already stored in Swift.
-
#read(output_stream = nil) ⇒ String, output_stream
Read the object data.
-
#relative_path ⇒ String
Returns the object's relative path (container name with object name).
-
#stream_to_file(output_path) ⇒ output_path
Stream the object data to a file.
-
#temp_url(expires = Time.now + 3600, method: :get) ⇒ String
Generates a public URL with an expiration time.
-
#url ⇒ String
Returns the object's URL.
-
#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
Write the object.
Methods inherited from Node
#clear_cache, #delete, #delete_if_exists, #exists?, #get_lines, #headers, #initialize, #metadata, #request, #service, #to_s
Methods included from Utils
Constructor Details
This class inherits a constructor from SwiftStorage::Node
Instance Attribute Details
#cache_control ⇒ String
Object cache control header.
15 16 17 |
# File 'lib/swift_storage/object.rb', line 15 def cache_control @cache_control end |
#content_length ⇒ String
Content length of the Object, in bytes.
15 16 17 |
# File 'lib/swift_storage/object.rb', line 15 def content_length @content_length end |
#content_type ⇒ String
Content type of the Object, eg: image/png
.
15 16 17 |
# File 'lib/swift_storage/object.rb', line 15 def content_type @content_type end |
#expires ⇒ String
When the object is set to expire.
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.
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
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_path ⇒ String
Returns the object's relative path (container name with object name)
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
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
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 |
#url ⇒ String
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
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
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.
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
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' : '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 |