Class: Awscli::S3::Directories

Inherits:
Object
  • Object
show all
Defined in:
lib/awscli/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, options = {}) ⇒ Directories

Returns a new instance of Directories.



213
214
215
# File 'lib/awscli/s3.rb', line 213

def initialize(connection, options = {})
  @conn = connection
end

Instance Method Details

#create(bucket_name, is_public) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# 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) ⇒ Object



232
233
234
235
236
237
238
239
# 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) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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) ⇒ Object



303
304
305
306
307
# 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) ⇒ Object



316
317
318
# File 'lib/awscli/s3.rb', line 316

def get_logging_status(dir_name)
  puts @conn.get_bucket_logging(dir_name).body['BucketLoggingStatus']
end

#listObject



217
218
219
# File 'lib/awscli/s3.rb', line 217

def list
  @conn.directories.table
end

#set_acl(dir_name, acl) ⇒ Object



309
310
311
312
313
314
# 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 = {}) ⇒ Object



320
321
322
# File 'lib/awscli/s3.rb', line 320

def set_logging_status(dir_name, logging_status = {})
  @conn.put_bucket_logging dir_name, logging_status
end