# coding: utf-8 require 'pathname' module Dag module CLI class Bucket < SubCommand desc 'list ([BUCKET])', 'show bucket/object list' option :prefix, type: :string, aliases: '-p', desc: "prefix filter", default: nil option :delimiter, type: :string, aliases: '-d', desc: "object delimiter", default: nil def list(bucket = nil) fields = [:name] unless bucket rows = handle_api_failure do client.buckets end else rows = handle_api_failure do bucket = client.buckets[bucket] bucket.objects.where(prefix: options[:prefix], delimiter: options[:delimiter]) end end handle_api_failure do rows.each do |row| puts row.name end end end desc 'read [BUCKET] [OBJECT]', 'read object' def read(bucket, object) rows = handle_api_failure do bucket = client.buckets[bucket] bucket.objects[object].read end print rows end desc 'create [BUCKET]', 'create bucket' def create(bucket) rows = handle_api_failure do client.create_bucket(bucket) end say_status("Create bucket", rows.name) end desc 'delete [BUCKET] ([OBJECT])', 'delete bucket or object' def delete(bucket, *objects) handle_api_failure do if objects.empty? # for bucket client.delete_bucket(bucket) say_status("Delete bucket", bucket) else # for objects th = [] objects.each do |object| th << Thread.new do client.delete_object(bucket, object) say_status("Delete object", File.join(bucket, object)) end end th.map(&:join) end end end desc 'write [BUCKET] [OBJECT]', 'write object / string or file path' option :data, type: :string, aliases: '-d', desc: "string or file path", required: true option :multipart, type: :boolean, aliases: '-m', desc: "multipart upload" option :multipart_jobs, type: :numeric, aliases: '-j', desc: "multipart upload jobs" option :multipart_splitsz, type: :numeric, aliases: '-s', desc: "multipart upload split size" def write(bucket, object) params = {} params.merge!(multipart: true) if options[:multipart] params.merge!(jobs: options[:multipart_jobs]) if options[:multipart_jobs] params.merge!(splitsz: options[:multipart_splitsz]) if options[:multipart_splitsz] data = File.exist?(options[:data]) ? Pathname.new(options[:data]) : options[:data] handle_api_failure do obj = client.buckets[bucket].objects[object] obj.write(data, params) say_status("Write object", File.join(bucket, object)) end end end end end