[英]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.