Class: Defog::File
- Inherits:
-
File
- Object
- File
- Defog::File
- Defined in:
- lib/defog/file.rb
Overview
Create a Defog::File proxy instance via Defog::Proxy#file, such as
defog = Defog::Proxy.new(:provider => :AWS, :aws_access_key_id => access_key, ...)
defog.file("key/to/my/file", "w") do |file|
# ... access the proxy file ...
end
or
file = defog.file("key/to/my/file", "w")
# ... access the proxy file ...
file.close
Defog::File inherits from ::File, so you can act on the proxy file using ordinary IO methods, such as
defog.file("key", "r") do |file|
file.readlines
end
You can also access the proxy file via its path, allowing things such as
defog.file("image100x100.jpg", "w") do |file|
system("convert souce.png -scale 100x100 #{file.path}")
end
(Note that the proxy file path has the same file extension as the cloud key string.)
Upon closing the proxy file, in normal use the cloud storage gets synchronized and the proxy deleted. See File#close for more details.
Instance Method Summary collapse
-
#close(opts = {}) ⇒ Object
Closes the proxy file and synchronizes the cloud storage (if it was opened as writeable) then deletes the proxy file.
- #create_proxy ⇒ Object
-
#initialize(opts = {}, &block) ⇒ File
constructor
:nodoc:.
- #upload_proxy ⇒ Object
Constructor Details
#initialize(opts = {}, &block) ⇒ File
:nodoc:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/defog/file.rb', line 35 def initialize(opts={}, &block) #:nodoc: opts = opts.keyword_args(:handle => :required, :mode => :required, :persist => :optional) @handle = opts.handle @persist = opts.persist key = @handle.key proxy_path = @handle.proxy_path proxy_path.dirname.mkpath case opts.mode when "r" create_proxy when "w", "w+" @upload = true when "r+", "a", "a+" create_proxy @upload = true else raise ArgumentError, "Invalid mode #{opts.mode.inspect}" end super(proxy_path, opts.mode, &block) end |
Instance Method Details
#close(opts = {}) ⇒ Object
Closes the proxy file and synchronizes the cloud storage (if it was opened as writeable) then deletes the proxy file.
Synchronization can be suppressed by passing the option
:synchronize => false
Synchronization will also be implicitly suppressed if the proxy file was deleted before this call, e.g., via ::File.unlink(file.path).
Whether the proxy file gets deleted vs persisted after the close can be set by passing the option
:persist => true or false
(This will override the setting of :persist passed to Proxy#file)
82 83 84 85 86 87 88 89 90 |
# File 'lib/defog/file.rb', line 82 def close(opts={}) opts = opts.keyword_args(:persist => @persist, :synchronize => true) super() if @handle.proxy_path.exist? upload_proxy if @upload and opts.synchronize @handle.proxy_path.unlink unless opts.persist end @handle.proxy.close_proxy_file(@handle) end |
#create_proxy ⇒ Object
58 59 60 61 |
# File 'lib/defog/file.rb', line 58 def create_proxy @handle.proxy.open_proxy_file(@handle) @handle.proxy.fog_wrapper.get_file(@handle.key, @handle.proxy_path) end |
#upload_proxy ⇒ Object
63 64 65 |
# File 'lib/defog/file.rb', line 63 def upload_proxy @handle.proxy.fog_wrapper.put_file(@handle.key, @handle.proxy_path) end |