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



1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
# File 'lib/aws/s3/client.rb', line 1206

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



1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
# File 'lib/aws/s3/client.rb', line 1347

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.



1243
1244
1245
1246
1247
1248
1249
# File 'lib/aws/s3/client.rb', line 1243

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



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/aws/s3/client.rb', line 1306

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



1267
1268
1269
1270
1271
# File 'lib/aws/s3/client.rb', line 1267

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



1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/aws/s3/client.rb', line 1295

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



1323
1324
1325
1326
1327
# File 'lib/aws/s3/client.rb', line 1323

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.



1187
1188
1189
# File 'lib/aws/s3/client.rb', line 1187

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

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



1251
1252
1253
1254
1255
1256
# File 'lib/aws/s3/client.rb', line 1251

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.



1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'lib/aws/s3/client.rb', line 1275

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



1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/aws/s3/client.rb', line 1258

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

#validate_parts!(parts) ⇒ Object



1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
# File 'lib/aws/s3/client.rb', line 1329

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