module MSS::S3::RegionDetection

@api private

Private Instance Methods

detect_region_and_retry(response) { || ... } click to toggle source
# File lib/mss/s3/region_detection.rb, line 25
def detect_region_and_retry(response, &retry_block)
  updgrade_to_v4(response, 'us-east-1')
  yield
  return if response.http_response.status == 200
  actual_region = region_from_location_header(response)
  updgrade_to_v4(response, actual_region)
  log_region_warning(response, actual_region)
  yield
end
log_region_warning(response, actual_region) click to toggle source
# File lib/mss/s3/region_detection.rb, line 64
def log_region_warning(response, actual_region)
  bucket_name = response.request_options[:bucket_name]
  S3::BUCKET_REGIONS[bucket_name] = actual_region
  log_warning("S3 client configured for #{@region.inspect} " +
    "but the bucket #{bucket_name.inspect} is in" +
    "#{actual_region.inspect}; Please configure the proper region " +
    "to avoid multiple unecessary redirects and signing attempts\n")
end
new_hostname(response, region) click to toggle source
# File lib/mss/s3/region_detection.rb, line 55
def new_hostname(response, region)
  bucket = response.request_options[:bucket_name]
  if region == 'us-east-1'
    's3-external-1.amazonmss.com'
  else
    "s3.#{region}.amazonmss.com"
  end
end
new_v4_signer(region) click to toggle source
# File lib/mss/s3/region_detection.rb, line 51
def new_v4_signer(region)
  Core::Signers::Version4.new(credential_provider, 's3', region)
end
region_from_location_header(response) click to toggle source
# File lib/mss/s3/region_detection.rb, line 46
def region_from_location_header(response)
  location = response.http_response.headers['location'].first
  location.match(/s3\.(.+?)\.amazonmss\.com/)[1]
end
requires_sigv4?(resp) click to toggle source
# File lib/mss/s3/region_detection.rb, line 19
def requires_sigv4?(resp)
  resp.http_response.status == 400 &&
  resp.http_response.body &&
  resp.http_response.body.include?('Please use MSS4-HMAC-SHA256')
end
retry_server_errors(&block) click to toggle source
Calls superclass method
# File lib/mss/s3/region_detection.rb, line 10
def retry_server_errors(&block)
  response = super
  if requires_sigv4?(response)
    detect_region_and_retry(response, &block)
  else
    response
  end
end
updgrade_to_v4(response, region) click to toggle source
# File lib/mss/s3/region_detection.rb, line 35
def updgrade_to_v4(response, region)
  bucket = response.request_options[:bucket_name]
  if response.http_request.body_stream.respond_to?(:rewind)
    response.http_request.body_stream.rewind
  end
  response.http_request.headers.delete('authorization')
  response.http_request.headers.delete('x-amz-security-token')
  response.http_request.host = new_hostname(response, region)
  new_v4_signer(region).sign_request(response.http_request)
end