Module: AWS::S3::Client::Validators
- Included in:
- AWS::S3::Client, AWS::S3::Client
- Defined in:
- lib/aws/s3/client.rb
Instance Method Summary collapse
-
#dns_compatible_bucket_name?(bucket_name) ⇒ Boolean
Returns true if the given
bucket_name
is DNS compatible. - #json_validation_message(obj) ⇒ Object
-
#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.
- #require_acl!(options) ⇒ Object
- #require_bucket_name!(bucket_name) ⇒ Object
- #require_part_number!(part_number) ⇒ Object
- #require_policy!(policy) ⇒ Object
- #require_upload_id!(upload_id) ⇒ Object
- #set_body_stream_and_content_length(request, options) ⇒ Object
-
#valid_bucket_name?(bucket_name) ⇒ Boolean
Returns true if the given bucket name is valid.
- #validate!(name, value, &block) ⇒ Object
-
#validate_bucket_name!(bucket_name) ⇒ Object
Returns true if the given bucket name is valid.
- #validate_key!(key) ⇒ Object
- #validate_parts!(parts) ⇒ Object
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/
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 |
# File 'lib/aws/s3/client.rb', line 1389 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
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 |
# File 'lib/aws/s3/client.rb', line 1550 def (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.
1426 1427 1428 1429 1430 1431 1432 |
# File 'lib/aws/s3/client.rb', line 1426 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
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 |
# File 'lib/aws/s3/client.rb', line 1489 def require_acl! = [ :acl, :grant_read, :grant_write, :grant_read_acp, :grant_write_acp, :grant_full_control, :access_control_policy, ] unless .keys.any?{|opt| .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
1450 1451 1452 1453 1454 |
# File 'lib/aws/s3/client.rb', line 1450 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
1526 1527 1528 1529 1530 |
# File 'lib/aws/s3/client.rb', line 1526 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
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
# File 'lib/aws/s3/client.rb', line 1478 def require_policy!(policy) validate!('policy', policy) do case when policy.nil? || policy == '' 'may not be blank' else (policy) end end end |
#require_upload_id!(upload_id) ⇒ Object
1520 1521 1522 1523 1524 |
# File 'lib/aws/s3/client.rb', line 1520 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
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 |
# File 'lib/aws/s3/client.rb', line 1506 def set_body_stream_and_content_length request, unless [: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'] = [:content_length] request.body_stream = [:data] end |
#valid_bucket_name?(bucket_name) ⇒ Boolean
Returns true if the given bucket name is valid.
1370 1371 1372 |
# File 'lib/aws/s3/client.rb', line 1370 def valid_bucket_name?(bucket_name) validate_bucket_name!(bucket_name) rescue false end |
#validate!(name, value, &block) ⇒ Object
1434 1435 1436 1437 1438 1439 |
# File 'lib/aws/s3/client.rb', line 1434 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.
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 |
# File 'lib/aws/s3/client.rb', line 1458 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
1441 1442 1443 1444 1445 1446 1447 1448 |
# File 'lib/aws/s3/client.rb', line 1441 def validate_key!(key) validate!('key', key) do case when key.nil? || key == '' 'may not be blank' end end end |
#validate_parts!(parts) ⇒ Object
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 |
# File 'lib/aws/s3/client.rb', line 1532 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 |