繁体   English   中英

使用ruby aws-sdk V2指示是否配置了静态网站

[英]Using ruby aws-sdk V2 to indicate if static website is configured

我为静态Web托管配置了许多S3存储桶,我试图使用Ruby aws-sdk遍历列表,以打印出各种属性,例如索引文档,错误文档,重定向规则等。

不幸的是,某些存储桶未配置为静态托管。 我需要跳过这些存储桶,以免出错。

我不知道如何检测是否为静态托管配置了存储桶,因此以下代码将引发错误。 见下文。

require 'aws-sdk'

s3 = Aws::S3::Resource.new
s3.buckets.take(3).each do |bucket|
  puts "Name = #{bucket.name}"
  puts "Index Document = #{bucket.website.index_document}"
end

由于未捕获而返回的错误是...

The specified bucket does not have a website configuration (Aws::S3::Errors::NoSuchWebsiteConfiguration)

我决定走捷径,只发现错误。

require 'aws-sdk'

s3 = Aws::S3::Resource.new

my_buckets = s3.buckets.map

my_buckets.each do |bucket|
  begin
    puts bucket.name
    puts bucket.website.error_document

  # if a website is not configured it's going to throw an error when you try to access bucket.website.error_document\
  # rescue the error, even though this seems like there should be a more elegant way to handle this.
  rescue Aws::S3::Errors::NoSuchWebsiteConfiguration => e
    puts e.message
  # Buckets that are just there to provide a redirect for www versions of the domain will also throw an error
  rescue Aws::S3::Errors::PermanentRedirect => e
    puts e.message
  rescue Exception => e
    puts e.message
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM