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



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/aws/s3/client.rb', line 1004

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

    # Bucket names should not contain underscores (_)
    bucket_name["_"] or

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

    # Bucket names should not end with a dash
    bucket_name[-1,1] == '-' 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



1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/aws/s3/client.rb', line 1144

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.



1041
1042
1043
1044
1045
1046
1047
# File 'lib/aws/s3/client.rb', line 1041

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

#require_acl!(acl) ⇒ Object



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/aws/s3/client.rb', line 1104

def require_acl!(acl)
  validate!('acl', acl) do
    case
    when acl.kind_of?(Hash)
      AccessControlList.new(acl).validate!
      nil
    when !acl.respond_to?(:to_str) && !acl.respond_to?(:to_xml)
      "must support to_xml: #{acl.inspect}"
    when acl.nil? || acl == ''
      'may not be blank'
    else
      xml_validation_message(acl)
    end
  end
end

#require_bucket_name!(bucket_name) ⇒ Object



1065
1066
1067
1068
1069
# File 'lib/aws/s3/client.rb', line 1065

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

#require_policy!(policy) ⇒ Object



1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/aws/s3/client.rb', line 1093

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



1120
1121
1122
1123
1124
# File 'lib/aws/s3/client.rb', line 1120

def require_upload_id!(upload_id)
  validate!("upload_id", upload_id) do
    "must not be blank" if upload_id.to_s.empty?
  end
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.



985
986
987
# File 'lib/aws/s3/client.rb', line 985

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

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



1049
1050
1051
1052
1053
1054
# File 'lib/aws/s3/client.rb', line 1049

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.



1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/aws/s3/client.rb', line 1073

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-z0-9._\-]+$/
      'may only contain lowercase letters, numbers, periods (.), ' +
      'underscores (_), and dashes (-)'
    when bucket_name !~ /^[a-z0-9]/
      'must start with a letter or a number'
    when !(3..255).include?(bucket_name.size)
      'must be between 3 and 255 characters long'
    when bucket_name =~ /(\d+\.){3}\d+/
      'must not be formatted like an IP address (e.g., 192.168.5.4)'
    when bucket_name =~ /\n/
      'must not contain a newline character'
    end
  end
end

#validate_key!(key) ⇒ Object



1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/aws/s3/client.rb', line 1056

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

#validate_parts!(parts) ⇒ Object



1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/aws/s3/client.rb', line 1126

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

#xml_validation_message(obj) ⇒ Object



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
# File 'lib/aws/s3/client.rb', line 1160

def xml_validation_message(obj)
  if obj.respond_to?(:to_str)
    obj = obj.to_str
  elsif obj.respond_to?(:to_xml)
    obj = obj.to_xml
  end

  error = nil
  begin
    REXML::Document.new(obj)
  rescue => e
    error = e
  end
  "contains invalid XML: #{error}" if error
end