Class: MLS::CLI::Storage::B2
- Inherits:
-
Object
- Object
- MLS::CLI::Storage::B2
- Defined in:
- lib/mls/cli/storage/b2.rb
Instance Method Summary collapse
- #copy_to_tempfile(key) ⇒ Object
- #delete(key) ⇒ Object
-
#destination(key) ⇒ Object
def host h = @configs || “s3.amazonaws.com/#href=":bucket">configs” h.delete_suffix(‘/’) end.
- #download(key, to = nil, &block) ⇒ Object
- #exists?(key) ⇒ Boolean
-
#initialize(configs = {}) ⇒ B2
constructor
A new instance of B2.
- #last_modified(key) ⇒ Object
- #local? ⇒ Boolean
- #mime_type(key) ⇒ Object
- #partition(value) ⇒ Object
- #sha1(key) ⇒ Object
- #write(key, file, meta_info) ⇒ Object
Constructor Details
#initialize(configs = {}) ⇒ B2
Returns a new instance of B2.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/mls/cli/storage/b2.rb', line 6 def initialize(configs = {}) @configs = configs @configs[:prefix] ||= "" @client = B2::Bucket.new({ 'bucketId' => configs[:bucket_id] }, B2.new({ account_id: configs[:account_id], application_key: configs[:application_key] })) end |
Instance Method Details
#copy_to_tempfile(key) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mls/cli/storage/b2.rb', line 54 def copy_to_tempfile(key) tmpfile = Tempfile.new([File.basename(key), File.extname(key)], binmode: true) download(key, tmpfile.path) if block_given? begin yield(tmpfile) ensure tmpfile.close! end else tmpfile end end |
#delete(key) ⇒ Object
50 51 52 |
# File 'lib/mls/cli/storage/b2.rb', line 50 def delete(key) @client.delete!(key) end |
#destination(key) ⇒ Object
def host
h = @configs[:bucket_host_alias] || "https://s3.amazonaws.com/#{@configs[:bucket]}"
h.delete_suffix('/')
end
31 32 33 |
# File 'lib/mls/cli/storage/b2.rb', line 31 def destination(key) "#{@configs[:prefix]}#{partition(key)}".gsub(/^\//, '') end |
#download(key, to = nil, &block) ⇒ Object
46 47 48 |
# File 'lib/mls/cli/storage/b2.rb', line 46 def download(key, to=nil, &block) @client.download(destination(key), to, &block) end |
#exists?(key) ⇒ Boolean
35 36 37 |
# File 'lib/mls/cli/storage/b2.rb', line 35 def exists?(key) @client.has_key?(destination(key)) end |
#last_modified(key) ⇒ Object
78 79 80 |
# File 'lib/mls/cli/storage/b2.rb', line 78 def last_modified(key) @client.file(destination(key)).uploaded_at end |
#local? ⇒ Boolean
18 19 20 |
# File 'lib/mls/cli/storage/b2.rb', line 18 def local? false end |
#mime_type(key) ⇒ Object
82 83 84 |
# File 'lib/mls/cli/storage/b2.rb', line 82 def mime_type(key) @client.file(destination(key)).mime_type end |
#partition(value) ⇒ Object
68 69 70 71 72 |
# File 'lib/mls/cli/storage/b2.rb', line 68 def partition(value) return value unless @configs[:partition] split = value.scan(/.{1,4}/) split.shift(@configs[:partition_depth] || 3).join("/") + split.join("") end |
#sha1(key) ⇒ Object
74 75 76 |
# File 'lib/mls/cli/storage/b2.rb', line 74 def sha1(key) @client.file(destination(key)).sha1 end |
#write(key, file, meta_info) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/mls/cli/storage/b2.rb', line 39 def write(key, file, ) file = file.tempfile if file.is_a?(ActionDispatch::Http::UploadedFile) file = File.open(file) if file.is_a?(String) @client.upload_file(key, file, mime_type: [:content_type], sha1: [:sha1], content_disposition: "inline; filename=\"#{[:filename]}\"") end |