self.with_region_detect

in s3_file/libraries/s3_file.rb [69:93]


  def self.with_region_detect(original_url, path, bucket, region)
    Chef::Log.info("Trying to download from #{original_url} in region #{region}")
    yield(original_url, region)
  rescue client::BadRequest, client::MovedPermanently, client::TemporaryRedirect => e
    region = e.response.headers[:x_amz_region] || e.response.headers[:x_amz_bucket_region]

    
    if region.nil?
      Chef::Log.warn("Could not download from S3: #{e.message}, trying to determine bucket region using head request")
      region = detect_bucket_region(bucket)

      
      if region.nil?
        Chef::Log.error("Could not determine the region of S3 bucket #{bucket}, please verify the S3 URL #{original_url}")
        raise
      end
    end

    Chef::Log.warn("First download try failed with '#{e.message}' (#{e.class}), retrying")

    url = [build_endpoint_url(bucket, region), path].join()
    Chef::Log.info("Retrying S3 download with new url #{url} in region #{region}")
    yield(url, region)
  end