You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

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



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/aws/s3/client.rb', line 357

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



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/aws/s3/client.rb', line 515

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.



395
396
397
398
399
400
401
# File 'lib/aws/s3/client.rb', line 395

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



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/aws/s3/client.rb', line 454

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_allowed_methods!(allowed_methods) ⇒ Object



531
532
533
534
535
536
537
538
539
# File 'lib/aws/s3/client.rb', line 531

def require_allowed_methods!(allowed_methods)
  validate!("allowed_methods", allowed_methods) do
    if !allowed_methods.kind_of?(Array)
      "must be an array"
    elsif !allowed_methods.all? { |x| x.kind_of?(String) }
      "must be an array of strings"
    end
  end
end

#require_allowed_origins!(allowed_origins) ⇒ Object



541
542
543
544
545
546
547
548
549
# File 'lib/aws/s3/client.rb', line 541

def require_allowed_origins!(allowed_origins)
  validate!("allowed_origins", allowed_origins) do
    if !allowed_origins.kind_of?(Array)
      "must be an array"
    elsif !allowed_origins.all? { |x| x.kind_of?(String) }
      "must be an array of strings"
    end
  end
end

#require_bucket_name!(bucket_name) ⇒ Object



419
420
421
422
423
# File 'lib/aws/s3/client.rb', line 419

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



491
492
493
494
495
# File 'lib/aws/s3/client.rb', line 491

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



443
444
445
446
447
448
449
450
451
452
# File 'lib/aws/s3/client.rb', line 443

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



485
486
487
488
489
# File 'lib/aws/s3/client.rb', line 485

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



471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/aws/s3/client.rb', line 471

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.



338
339
340
# File 'lib/aws/s3/client.rb', line 338

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

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



403
404
405
406
407
408
# File 'lib/aws/s3/client.rb', line 403

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.



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/aws/s3/client.rb', line 427

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



410
411
412
413
414
415
416
417
# File 'lib/aws/s3/client.rb', line 410

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

#validate_parts!(parts) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/aws/s3/client.rb', line 497

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