当使用Ruby v1.0的AWS SDK时,检查simpleDB域是否存在很简单:
def domain_exists?(domain_name)
sdb = AWS::SimpleDB.new
domain = sdb.domains[domain_name]
domain.exists?
end
然而,使用Ruby的AWS SDK v2.0就不再可能了。如何使用v2.0检查simpleDB域是否存在?
有两种方法。
-
使用
domain_metadata
并捕获异常。def domain_exists?(domain_name) sdb = Aws::SimpleDB::Client.new sdb.domain_metadata(domain_name: domain_name) return true rescue Aws::SimpleDB::Errors::NoSuchDomain return false end
-
重新打开
Aws::SimpleDB::Client
类,增加一个使用list_domains
的递归方法domain_exists?
class Aws::SimpleDB::Client def domain_exists?(domain_name, limit = 100, next_token=nil) resp = list_domains(next_token: next_token, max_number_of_domains: limit) domain_exists = resp.domain_names.include?(domain_name) return domain_exists if domain_exists # found the domain return domain_exists if resp.next_token.nil? # no more domains to search domain_exists?(domain_name, limit, resp.next_token) # more domains to search end end
然后就变得很简单了:
def domain_exists?(domain_name, limit = 100) sdb = Aws::SimpleDB::Client.new sdb.domain_exists?(domain_name, limit) end