Module: AWS::S3::Client::Validators

Included in:
AWS::S3::Client, AWS::S3::Client
Defined in:
lib/aws/s3/client.rb

Instance Method Summary collapse

Instance Method Details

#dns_compatible_bucket_name?(bucket_name) ⇒ Boolean

Returns true if the given bucket_name is DNS compatible.

DNS compatible bucket names may be accessed like:

http://dns.compat.bucket.name.s3.amazonaws.com/

Whereas non-dns compatible bucket names must place the bucket name in the url path, like:

http://s3.amazonaws.com/dns_incompat_bucket_name/

Returns:

  • (Boolean)

    Returns true if the given bucket name may be is dns compatible. this bucket n



1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'lib/aws/s3/client.rb', line 1522

def dns_compatible_bucket_name?(bucket_name)
  return false if
    !valid_bucket_name?(bucket_name) or

    # Bucket names should be between 3 and 63 characters long
    bucket_name.size > 63 or

    # Bucket names must only contain lowercase letters, numbers, dots, and dashes
    # and must start and end with a lowercase letter or a number
    bucket_name !~ /^[a-z0-9][a-z0-9.-]+[a-z0-9]$/ or

    # Bucket names should not be formatted like an IP address (e.g., 192.168.5.4)
    bucket_name =~ /(\d+\.){3}\d+/ or

    # Bucket names cannot contain two, adjacent periods
    bucket_name['..'] or

    # Bucket names cannot contain dashes next to periods
    # (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
    (bucket_name['-.'] || bucket_name['.-'])

  true
end

#json_validation_message(obj) ⇒ Object



1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
# File 'lib/aws/s3/client.rb', line 1680

def json_validation_message(obj)
  if obj.respond_to?(:to_str)
    obj = obj.to_str
  elsif obj.respond_to?(:to_json)
    obj = obj.to_json
  end

  error = nil
  begin
    JSON.parse(obj)
  rescue => e
    error = e
  end
  "contains invalid JSON: #{error}" if error
end

#path_style_bucket_name?(bucket_name) ⇒ Boolean

Returns true if the bucket name must be used in the request path instead of as a sub-domain when making requests against S3.

This can be an issue if the bucket name is DNS compatible but contains '.' (periods). These cause the SSL certificate to become invalid when making authenticated requets over SSL to the bucket name. The solution is to send this as a path argument instead.

Returns:

  • (Boolean)

    Returns true if the bucket name should be used as a path segement instead of dns prefix when making requests against s3.



1560
1561
1562
1563
1564
1565
1566
# File 'lib/aws/s3/client.rb', line 1560

def path_style_bucket_name? bucket_name
  if dns_compatible_bucket_name?(bucket_name)
    bucket_name =~ /\./ ? true : false
  else
    true
  end
end

#require_acl!(options) ⇒ Object



1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
# File 'lib/aws/s3/client.rb', line 1619

def require_acl! options
  acl_options = [
    :acl,
    :grant_read,
    :grant_write,
    :grant_read_acp,
    :grant_write_acp,
    :grant_full_control,
    :access_control_policy,
  ]
  unless options.keys.any?{|opt| acl_options.include?(opt) }
    msg = "missing a required ACL option, must provide an ACL " +
          "via :acl, :grant_* or :access_control_policy"
    raise ArgumentError, msg
  end
end

#require_bucket_name!(bucket_name) ⇒ Object



1584
1585
1586
1587
1588
# File 'lib/aws/s3/client.rb', line 1584

def require_bucket_name! bucket_name
  if [nil, ''].include?(bucket_name)
    raise ArgumentError, "bucket_name may not be blank"
  end
end

#require_part_number!(part_number) ⇒ Object



1656
1657
1658
1659
1660
# File 'lib/aws/s3/client.rb', line 1656

def require_part_number! part_number
  validate!("part_number", part_number) do
    "must not be blank" if part_number.to_s.empty?
  end
end

#require_policy!(policy) ⇒ Object



1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
# File 'lib/aws/s3/client.rb', line 1608

def require_policy!(policy)
  validate!('policy', policy) do
    case
    when policy.nil? || policy == ''
      'may not be blank'
    else
      json_validation_message(policy)
    end
  end
end

#require_upload_id!(upload_id) ⇒ Object



1650
1651
1652
1653
1654
# File 'lib/aws/s3/client.rb', line 1650

def require_upload_id!(upload_id)
  validate!("upload_id", upload_id) do
    "must not be blank" if upload_id.to_s.empty?
  end
end

#set_body_stream_and_content_length(request, options) ⇒ Object



1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
# File 'lib/aws/s3/client.rb', line 1636

def set_body_stream_and_content_length request, options

  unless options[:content_length]
    msg = "S3 requires a content-length header, unable to determine "
    msg << "the content length of the data provided, please set "
    msg << ":content_length"
    raise ArgumentError, msg
  end

  request.headers['content-length'] = options[:content_length]
  request.body_stream = options[:data]

end

#valid_bucket_name?(bucket_name) ⇒ Boolean

Returns true if the given bucket name is valid.

Returns:

  • (Boolean)

    Returns true if the given bucket name is valid.



1503
1504
1505
# File 'lib/aws/s3/client.rb', line 1503

def valid_bucket_name?(bucket_name)
  validate_bucket_name!(bucket_name) rescue false
end

#validate!(name, value, &block) ⇒ Object



1568
1569
1570
1571
1572
1573
# File 'lib/aws/s3/client.rb', line 1568

def validate! name, value, &block
  if error_msg = yield
    raise ArgumentError, "#{name} #{error_msg}"
  end
  value
end

#validate_bucket_name!(bucket_name) ⇒ Object

Returns true if the given bucket name is valid. If the name is invalid, an ArgumentError is raised.



1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
# File 'lib/aws/s3/client.rb', line 1592

def validate_bucket_name!(bucket_name)
  validate!('bucket_name', bucket_name) do
    case
    when bucket_name.nil? || bucket_name == ''
      'may not be blank'
    when bucket_name !~ /^[A-Za-z0-9._\-]+$/
      'may only contain uppercase letters, lowercase letters, numbers, periods (.), ' +
      'underscores (_), and dashes (-)'
    when !(3..255).include?(bucket_name.size)
      'must be between 3 and 255 characters long'
    when bucket_name =~ /\n/
      'must not contain a newline character'
    end
  end
end

#validate_key!(key) ⇒ Object



1575
1576
1577
1578
1579
1580
1581
1582
# File 'lib/aws/s3/client.rb', line 1575

def validate_key!(key)
  validate!('key', key) do
    case
    when key.nil? || key == ''
      'may not be blank'
    end
  end
end

#validate_parts!(parts) ⇒ Object



1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/aws/s3/client.rb', line 1662

def validate_parts!(parts)
  validate!("parts", parts) do
    if !parts.kind_of?(Array)
      "must not be blank"
    elsif parts.empty?
      "must contain at least one entry"
    elsif !parts.all? { |p| p.kind_of?(Hash) }
      "must be an array of hashes"
    elsif !parts.all? { |p| p[:part_number] }
      "must contain part_number for each part"
    elsif !parts.all? { |p| p[:etag] }
      "must contain etag for each part"
    elsif parts.any? { |p| p[:part_number].to_i < 1 }
      "must not have part numbers less than 1"
    end
  end
end