class Awscli::S3::Directories

Public Class Methods

new(connection, options = {}) click to toggle source
# File lib/awscli/s3.rb, line 213
def initialize(connection, options = {})
  @conn = connection
end

Public Instance Methods

create(bucket_name, is_public) click to toggle source
# File lib/awscli/s3.rb, line 221
def create(bucket_name, is_public)
  dir = @conn.directories.create(
    :key => bucket_name,
    :public => is_public
  )
rescue Excon::Errors::Conflict
  puts "Bucket already exists, bucket name should be unique globally"
else
  puts "Created bucket: #{dir.key}"
end
delete(dir_name) click to toggle source
# File lib/awscli/s3.rb, line 232
def delete dir_name
  dir = @conn.directories.get(dir_name)
  abort "Cannot find bucket #{dir_name}" unless dir
  #check if the dir is empty or not
  abort "Bucket is not empty, use rec_delete to force delete bucket" if dir.files.length != 0
  dir.destroy
  puts "Deleted Bucket: #{dir_name}"
end
delete_rec(dir_name) click to toggle source
# File lib/awscli/s3.rb, line 241
def delete_rec(dir_name)
  #Forked from https://gist.github.com/bdunagan/1383301
  data_queue = Queue.new
  semaphore = Mutex.new
  threads = Array.new
  total_listed = 0
  total_deleted = 0
  thread_count = 20 #num_of_threads to perform deletion
  dir = @conn.directories.get(dir_name)
  abort "Cannot find bucket #{dir_name}" unless dir
  if dir.files.length != 0
    if agree("Are you sure want to delete all the objects in the bucket ?  ", true)
      puts
      puts "==Deleting all the files in '#{dir_name}'=="
      #fetch files in the bucket
      threads << Thread.new do
        Thread.current[:name] = "get files"
        puts "...started thread '#{Thread.current[:name]}'...\n"
        # Get all the files from this bucket. Fog handles pagination internally
        dir.files.all.each do |file|
          data_queue.enq(file) #add the file into the queue
          total_listed += 1
        end
        # Add a final EOF message to signal the deletion threads to stop.
        thread_count.times {data_queue.enq(:EOF)}
      end
      # Delete all the files in the queue until EOF with N threads.
      thread_count.times do |count|
        threads << Thread.new(count) do |number|
          Thread.current[:name] = "delete files(#{number})"
          puts "...started thread '#{Thread.current[:name]}'...\n"
          # Dequeue until EOF.
          file = nil
          while file != :EOF
            # Dequeue the latest file and delete it. (Will block until it gets a new file.)
            file = data_queue.deq
            file.destroy if file != :EOF
            # Increment the global synchronized counter.
            semaphore.synchronize {total_deleted += 1}
            puts "Deleted #{total_deleted} out of #{total_listed}\n" if (rand(100) == 1)
          end
        end
      end
      # Wait for the threads to finish.
      threads.each do |t|
        begin
          t.join
        rescue RuntimeError => e
          puts "Failure on thread #{t[:name]}: #{e.message}"
        end
      end
      #finally delete the bucket it self
      dir.destroy
      puts "Deleted bucket: #{dir_name} and all its contents"
    end
  else
    #empty bucket
    dir.destroy
    puts "Deleted bucket: #{dir_name}"
  end
end
get_acl(dir_name) click to toggle source
# File lib/awscli/s3.rb, line 303
def get_acl(dir_name)
  dir = @conn.directories.get(dir_name)
  abort "Cannot find bucket #{dir_name}" unless dir
  puts dir.acl
end
get_logging_status(dir_name) click to toggle source
# File lib/awscli/s3.rb, line 316
def get_logging_status(dir_name)
  puts @conn.get_bucket_logging(dir_name).body['BucketLoggingStatus']
end
list() click to toggle source
# File lib/awscli/s3.rb, line 217
def list
  @conn.directories.table
end
set_acl(dir_name, acl) click to toggle source
# File lib/awscli/s3.rb, line 309
def set_acl(dir_name, acl)
  dir = @conn.directories.get(dir_name)
  abort "Cannot find bucket #{dir_name}" unless dir
  dir.acl = acl
  puts "Acl has been changed to #{acl}"
end
set_logging_status(dir_name, logging_status = {}) click to toggle source
# File lib/awscli/s3.rb, line 320
def set_logging_status(dir_name, logging_status = {})
  @conn.put_bucket_logging dir_name, logging_status
end