ruby 用于删除所有云文件和容器的Ruby脚本 - 也就是关闭帐户。需要宝石'雾'和'哑剧类型'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于删除所有云文件和容器的Ruby脚本 - 也就是关闭帐户。需要宝石'雾'和'哑剧类型'相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby

require 'fog'

def delete_file(container, file_num, max_tries)
  max_retries ||= max_tries
  try = 0
  puts "(#{file_num} of #{container.files.count}) Removing #{container.files[file_num].key}"
  begin
    container.files[file_num].destroy
  rescue Excon::Errors::NotFound, Excon::Errors::Timeout, Fog::Storage::Rackspace::NotFound => e
    if try == max_retries
      $stderr.puts e.message
    else
      try += 1
      puts "Retry \##{try}"
      retry
    end
  end
end

def equal_div(first, last, num_of_groups)
  total      = last - first
  group_size = total / num_of_groups + 1

  top    = first
  bottom = top + group_size
  blocks = 1.upto(num_of_groups).inject([]) do |result, x|
    bottom = last if bottom > last
    result << [ top, bottom ]

    top    += group_size + 1
    bottom =  top + group_size

    result
  end

  blocks
end

service = Fog::Storage.new({
    :provider             => 'Rackspace',               # Rackspace Fog provider
    :rackspace_username   => 'username', # Your Rackspace Username
    :rackspace_api_key    => 'key',            # Your Rackspace API key
    :rackspace_region     => :dfw,                      # Defaults to :dfw
    :connection_options   => {},                        # Optional
    :rackspace_servicenet => false                      # Optional, only use if you're the Rackspace Region Data Center
})

containers = service.directories.select do |s|
  s.key =~ /^lb*/  # Only delete containers that match the regexp
end

TOT_THREADS = 4
threads     = []


containers.each do |container|
    begin
  puts
  puts "-----------------------------------------"
  puts "-- Removing _ALL_ objects from #{container.key}"
  puts "-----------------------------------------"
  puts

  #puts "container.files.count: #{container.files.count}"

  ## separates the number of files into equal groups to distribute to each thread
  mygroups = equal_div(0, container.files.count - 1, TOT_THREADS)

  0.upto(TOT_THREADS - 1) do |thread|
    threads << Thread.new([ container, mygroups[thread] ]) { |tObject|
      tObject[1][0].upto(tObject[1][1]) do |x|
        delete_file(tObject[0], x, 5)
      end
    }

  end
  threads.each { |aThread|  aThread.join }
  puts "Deleting #{container.key}"
  container.destroy
  rescue
      next    # do_something_* again, with the next i
  end
end

以上是关于ruby 用于删除所有云文件和容器的Ruby脚本 - 也就是关闭帐户。需要宝石'雾'和'哑剧类型'的主要内容,如果未能解决你的问题,请参考以下文章

ruby 这个Ruby脚本将批量删除超过30天的所有Slack文件。只需从https://api.slack.com/web#authentication添加您的API令牌即可

如何使用Ruby-Rails删除文件夹的所有内容?

用于检查应用程序中有多少行代码的 Ruby 脚本

使用 Powershell 从 Azure 存储子容器中删除所有文件

ruby 用于从给定的aws帐户备份所有simpledb表的脚本

ruby 用于将.mdb db文件转换为.csv的Ruby脚本