1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
|
# -*- coding: utf-8 -*-
# Copyright 2011-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
require 'rexml/document'
require 'pathname'
require 'stringio'
require 'json'
require 'digest/md5'
require 'base64'
require 'nokogiri'
require 'set'
module AWS
class S3
# Client class for Amazon Simple Storage Service (S3).
class Client < Core::Client
include RegionDetection
def initialize(options = {})
super(options.merge(:http_continue_threshold => 0))
end
signature_version :S3
API_VERSION = '2006-03-01'
XMLNS = "http://s3.amazonaws.com/doc/#{API_VERSION}/"
HTTP_200_ERROR_OPERATIONS = Set.new([
:complete_multipart_upload,
:copy_object,
:copy_part,
])
autoload :XML, 'aws/s3/client/xml'
# @api private
EMPTY_BODY_ERRORS = {
304 => Errors::NotModified,
403 => Errors::Forbidden,
400 => Errors::BadRequest,
404 => Errors::NoSuchKey,
}
# @api private
CACHEABLE_REQUESTS = Set[]
include DataOptions
include Core::UriEscape
# @param [Core::Http::Request] request
# @api private
def sign_request request
case @config.s3_signature_version.to_sym
when :v4 then v4_signer.sign_request(request)
when :v3 then v3_signer.sign_request(request)
else
raise "invalid signature version #{@config.s3_signature_version.inspect}"
end
end
protected
# @return [Core::Signers::S3]
def v3_signer
@v3_signer ||= Core::Signers::S3.new(credential_provider)
end
# @return [Core::Signers::Version4]
def v4_signer
@v4_signer ||= begin
Core::Signers::Version4.new(credential_provider, 's3', @region)
end
end
# @param [Http::Request] req
# @return [Boolean]
def chunk_sign? req
req.http_method == 'PUT' &&
req.headers['content-length'].to_i > 2 * 1024 * 1024 # 2MB
end
def self.bucket_method(method_name, verb, *args, &block)
method_options = (args.pop if args.last.kind_of?(Hash)) || {}
xml_grammar = (args.pop if args.last.respond_to?(:rules))
verb = verb.to_s.upcase
subresource = args.first
add_client_request_method(method_name) do
configure_request do |req, options|
require_bucket_name!(options[:bucket_name])
req.http_method = verb
req.bucket = options[:bucket_name]
req.add_param(subresource) if subresource
if header_options = method_options[:header_options]
header_options.each do |(opt, header)|
if value = options[opt]
# for backwards compatability we translate canned acls
# header values from symbols to strings (e.g.
# :public_read translates to 'public-read')
value = (opt == :acl ? value.to_s.tr('_', '-') : value)
req.headers[header] = value
end
end
end
end
instance_eval(&block) if block
if xml_grammar
parser = Core::XML::Parser.new(xml_grammar.rules)
process_response do |resp|
resp.data = parser.parse(resp.http_response.body)
super(resp)
end
simulate_response do |resp|
resp.data = parser.simulate
super(resp)
end
end
end
end
protected
def set_metadata request, options
if metadata = options[:metadata]
Array(metadata).each do |name, value|
request.headers["x-amz-meta-#{name}"] = value
end
end
end
def set_storage_class request, options
storage_class = options[:storage_class]
if storage_class.kind_of?(Symbol)
request.headers["x-amz-storage-class"] = storage_class.to_s.upcase
elsif storage_class
request.headers["x-amz-storage-class"] = storage_class
end
end
def set_server_side_encryption request, options
sse = options[:server_side_encryption]
if sse.is_a?(Symbol)
request.headers['x-amz-server-side-encryption'] = sse.to_s.upcase
elsif sse
request.headers['x-amz-server-side-encryption'] = sse
end
end
def extract_error_details response
if
(
response.http_response.status >= 300 ||
HTTP_200_ERROR_OPERATIONS.include?(response.request_type)
) and
body = response.http_response.body and
error = Core::XML::Parser.parse(body) and
error[:code]
then
[error[:code], error[:message]]
end
end
def empty_response_body? response_body
response_body.nil? or response_body == ''
end
# There are a few of s3 requests that can generate empty bodies and
# yet still be errors. These return empty bodies to comply with the
# HTTP spec. We have to detect these errors specially.
def populate_error resp
code = resp.http_response.status
if EMPTY_BODY_ERRORS.include?(code) and empty_response_body?(resp.http_response.body)
error_class = EMPTY_BODY_ERRORS[code]
resp.error = error_class.new(resp.http_request, resp.http_response)
else
super
end
end
def retryable_error? response
super ||
http_200_error?(response) ||
response.error.is_a?(Errors::RequestTimeout)
end
# S3 may return with a 200 status code in the response, but still
# embed an error in the body for the following operations:
#
# * `#complete_multipart_upload`
# * `#copy_object`
# * `#copy_part`
#
# To ensure the response is not in error, we have to parse
# it before the normal parser.
def http_200_error? response
HTTP_200_ERROR_OPERATIONS.include?(response.request_type) &&
extract_error_details(response)
end
def new_request
req = S3::Request.new
req.force_path_style = config.s3_force_path_style?
req
end
# Previously the access control policy could be specified via :acl
# as a string or an object that responds to #to_xml. The prefered
# method now is to pass :access_control_policy an xml document.
def move_access_control_policy options
if acl = options[:acl]
if acl.is_a?(String) and is_xml?(acl)
options[:access_control_policy] = options.delete(:acl)
elsif acl.respond_to?(:to_xml)
options[:access_control_policy] = options.delete(:acl).to_xml
end
end
end
# @param [String] possible_xml
# @return [Boolean] Returns `true` if the given string is a valid xml
# document.
def is_xml? possible_xml
begin
REXML::Document.new(possible_xml).has_elements?
rescue
false
end
end
def md5 str
Base64.encode64(OpenSSL::Digest::MD5.digest(str)).strip
end
def parse_copy_part_response resp
doc = REXML::Document.new(resp.http_response.body)
resp[:etag] = doc.root.elements["ETag"].text
resp[:last_modified] = doc.root.elements["LastModified"].text
if header = resp.http_response.headers['x-amzn-requestid']
data[:request_id] = [header].flatten.first
end
end
def extract_object_headers resp
meta = {}
resp.http_response.headers.each_pair do |name,value|
if name =~ /^x-amz-meta-(.+)$/i
meta[$1] = [value].flatten.join
end
end
resp.data[:meta] = meta
if expiry = resp.http_response.headers['x-amz-expiration']
expiry.first =~ /^expiry-date="(.+)", rule-id="(.+)"$/
exp_date = DateTime.parse($1)
exp_rule_id = $2
else
exp_date = nil
exp_rule_id = nil
end
resp.data[:expiration_date] = exp_date if exp_date
resp.data[:expiration_rule_id] = exp_rule_id if exp_rule_id
restoring = false
restore_date = nil
if restore = resp.http_response.headers['x-amz-restore']
if restore.first =~ /ongoing-request="(.+?)", expiry-date="(.+?)"/
restoring = $1 == "true"
restore_date = $2 && DateTime.parse($2)
elsif restore.first =~ /ongoing-request="(.+?)"/
restoring = $1 == "true"
end
end
resp.data[:restore_in_progress] = restoring
resp.data[:restore_expiration_date] = restore_date if restore_date
{
'x-amz-version-id' => :version_id,
'content-type' => :content_type,
'content-encoding' => :content_encoding,
'cache-control' => :cache_control,
'expires' => :expires,
'etag' => :etag,
'x-amz-website-redirect-location' => :website_redirect_location,
'accept-ranges' => :accept_ranges,
'x-amz-server-side-encryption-customer-algorithm' => :sse_customer_algorithm,
'x-amz-server-side-encryption-customer-key-MD5' => :sse_customer_key_md5
}.each_pair do |header,method|
if value = resp.http_response.header(header)
resp.data[method] = value
end
end
if time = resp.http_response.header('Last-Modified')
resp.data[:last_modified] = Time.parse(time)
end
if length = resp.http_response.header('content-length')
resp.data[:content_length] = length.to_i
end
if sse = resp.http_response.header('x-amz-server-side-encryption')
resp.data[:server_side_encryption] = sse.downcase.to_sym
end
end
module Validators
# @return [Boolean] Returns true if the given bucket name is valid.
def valid_bucket_name?(bucket_name)
validate_bucket_name!(bucket_name) rescue false
end
# 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/
#
# @return [Boolean] Returns true if the given bucket name may be
# is dns compatible.
# this bucket n
#
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
# 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.
#
# @return [Boolean] Returns true if the bucket name should be used
# as a path segement instead of dns prefix when making requests
# against s3.
#
def path_style_bucket_name? bucket_name
if dns_compatible_bucket_name?(bucket_name)
bucket_name =~ /\./ ? true : false
else
true
end
end
def validate! name, value, &block
if error_msg = yield
raise ArgumentError, "#{name} #{error_msg}"
end
value
end
def validate_key!(key)
validate!('key', key) do
case
when key.nil? || key == ''
'may not be blank'
end
end
end
def require_bucket_name! bucket_name
if [nil, ''].include?(bucket_name)
raise ArgumentError, "bucket_name may not be blank"
end
end
# Returns true if the given bucket name is valid. If the name
# is invalid, an ArgumentError is raised.
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
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
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
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
def require_upload_id!(upload_id)
validate!("upload_id", upload_id) do
"must not be blank" if upload_id.to_s.empty?
end
end
def require_part_number! part_number
validate!("part_number", part_number) do
"must not be blank" if part_number.to_s.empty?
end
end
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
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
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
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
end
include Validators
extend Validators
end
class Client::V20060301 < Client
def self.object_method(method_name, verb, *args, &block)
bucket_method(method_name, verb, *args) do
configure_request do |req, options|
validate_key!(options[:key])
super(req, options)
req.key = options[:key]
end
instance_eval(&block) if block
end
end
public
# Creates a bucket.
# @overload create_bucket(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @return [Core::Response]
bucket_method(:create_bucket, :put, :header_options => {
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
}) do
configure_request do |req, options|
validate_bucket_name!(options[:bucket_name])
if location = options[:location_constraint]
xmlns = "http://s3.amazonaws.com/doc/#{API_VERSION}/"
req.body = <<-XML
<CreateBucketConfiguration xmlns="#{xmlns}">
<LocationConstraint>#{location}</LocationConstraint>
</CreateBucketConfiguration>
XML
end
super(req, options)
end
end
alias_method :put_bucket, :create_bucket
# @!method put_bucket_website(options = {})
# @param [Hash] options
# @option (see WebsiteConfiguration#initialize)
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:put_bucket_website, :put, 'website') do
configure_request do |req, options|
req.body = Nokogiri::XML::Builder.new do |xml|
xml.WebsiteConfiguration(:xmlns => XMLNS) do
if redirect = options[:redirect_all_requests_to]
xml.RedirectAllRequestsTo do
xml.HostName(redirect[:host_name])
xml.Protocol(redirect[:protocol]) if redirect[:protocol]
end
end
if indx = options[:index_document]
xml.IndexDocument do
xml.Suffix(indx[:suffix])
end
end
if err = options[:error_document]
xml.ErrorDocument do
xml.Key(err[:key])
end
end
rules = options[:routing_rules]
if rules.is_a?(Array) && !rules.empty?
xml.RoutingRules do
rules.each do |rule|
xml.RoutingRule do
redirect = rule[:redirect]
xml.Redirect do
xml.Protocol(redirect[:protocol]) if redirect[:protocol]
xml.HostName(redirect[:host_name]) if redirect[:host_name]
xml.ReplaceKeyPrefixWith(redirect[:replace_key_prefix_with]) if redirect[:replace_key_prefix_with]
xml.ReplaceKeyWith(redirect[:replace_key_with]) if redirect[:replace_key_with]
xml.HttpRedirectCode(redirect[:http_redirect_code]) if redirect[:http_redirect_code]
end
if condition = rule[:condition]
xml.Condition do
xml.KeyPrefixEquals(condition[:key_prefix_equals]) if condition[:key_prefix_equals]
xml.HttpErrorCodeReturnedEquals(condition[:http_error_code_returned_equals]) if condition[:http_error_code_returned_equals]
end
end
end
end
end
end
end
end.doc.root.to_xml
super(req, options)
end
end
# @overload get_bucket_website(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
# * `:index_document` - (Hash)
# * `:suffix` - (String)
# * `:error_document` - (Hash)
# * `:key` - (String)
bucket_method(:get_bucket_website, :get, 'website', XML::GetBucketWebsite)
# @overload delete_bucket_website(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket_website, :delete, 'website')
# Deletes an empty bucket.
# @overload delete_bucket(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket, :delete)
# @overload set_bucket_lifecycle_configuration(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :lifecycle_configuration
# @return [Core::Response]
bucket_method(:set_bucket_lifecycle_configuration, :put) do
configure_request do |req, options|
xml = options[:lifecycle_configuration]
req.add_param('lifecycle')
req.body = xml
req.headers['content-md5'] = md5(xml)
super(req, options)
end
end
# @overload get_bucket_lifecycle_configuration(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_lifecycle_configuration, :get) do
configure_request do |req, options|
req.add_param('lifecycle')
super(req, options)
end
process_response do |resp|
xml = resp.http_response.body
resp.data = XML::GetBucketLifecycleConfiguration.parse(xml)
end
end
# @overload delete_bucket_lifecycle_configuration(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket_lifecycle_configuration, :delete) do
configure_request do |req, options|
req.add_param('lifecycle')
super(req, options)
end
end
# @overload put_bucket_cors(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,Array<Hash>] :rules An array of rule hashes.
# * `:id` - (String) A unique identifier for the rule. The ID
# value can be up to 255 characters long. The IDs help you find
# a rule in the configuration.
# * `:allowed_methods` - (required,Array<String>) A list of HTTP
# methods that you want to allow the origin to execute.
# Each rule must identify at least one method.
# * `:allowed_origins` - (required,Array<String>) A list of origins
# you want to allow cross-domain requests from. This can
# contain at most one * wild character.
# * `:allowed_headers` - (Array<String>) A list of headers allowed
# in a pre-flight OPTIONS request via the
# Access-Control-Request-Headers header. Each header name
# specified in the Access-Control-Request-Headers header must
# have a corresponding entry in the rule.
# Amazon S3 will send only the allowed headers in a response
# that were requested. This can contain at most one * wild
# character.
# * `:max_age_seconds` - (Integer) The time in seconds that your
# browser is to cache the preflight response for the specified
# resource.
# * `:expose_headers` - (Array<String>) One or more headers in
# the response that you want customers to be able to access
# from their applications (for example, from a JavaScript
# XMLHttpRequest object).
# @return [Core::Response]
bucket_method(:put_bucket_cors, :put) do
configure_request do |req, options|
req.add_param('cors')
options[:rules].each do |rule|
require_allowed_methods!(rule[:allowed_methods])
require_allowed_origins!(rule[:allowed_origins])
end
xml = Nokogiri::XML::Builder.new do |xml|
xml.CORSConfiguration do
options[:rules].each do |rule|
xml.CORSRule do
xml.ID(rule[:id]) if rule[:id]
(rule[:allowed_methods] || []).each do |method|
xml.AllowedMethod(method)
end
(rule[:allowed_origins] || []).each do |origin|
xml.AllowedOrigin(origin)
end
(rule[:allowed_headers] || []).each do |header|
xml.AllowedHeader(header)
end
xml.MaxAgeSeconds(rule[:max_age_seconds]) if
rule[:max_age_seconds]
(rule[:expose_headers] || []).each do |header|
xml.ExposeHeader(header)
end
end
end
end
end.doc.root.to_xml
req.body = xml
req.headers['content-md5'] = md5(xml)
super(req, options)
end
end
# @overload get_bucket_cors(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_cors, :get) do
configure_request do |req, options|
req.add_param('cors')
super(req, options)
end
process_response do |resp|
resp.data = XML::GetBucketCors.parse(resp.http_response.body)
end
end
# @overload delete_bucket_cors(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket_cors, :delete) do
configure_request do |req, options|
req.add_param('cors')
super(req, options)
end
end
# @overload put_bucket_tagging(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [Hash] :tags
# @return [Core::Response]
bucket_method(:put_bucket_tagging, :put) do
configure_request do |req, options|
req.add_param('tagging')
xml = Nokogiri::XML::Builder.new
xml.Tagging do |xml|
xml.TagSet do
options[:tags].each_pair do |key,value|
xml.Tag do
xml.Key(key)
xml.Value(value)
end
end
end
end
xml = xml.doc.root.to_xml
req.body = xml
req.headers['content-md5'] = md5(xml)
super(req, options)
end
end
# @overload get_bucket_tagging(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_tagging, :get) do
configure_request do |req, options|
req.add_param('tagging')
super(req, options)
end
process_response do |resp|
resp.data = XML::GetBucketTagging.parse(resp.http_response.body)
end
end
# @overload delete_bucket_tagging(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket_tagging, :delete) do
configure_request do |req, options|
req.add_param('tagging')
super(req, options)
end
end
# @overload list_buckets(options = {})
# @param [Hash] options
# @return [Core::Response]
add_client_request_method(:list_buckets) do
configure_request do |req, options|
req.http_method = "GET"
end
process_response do |resp|
resp.data = XML::ListBuckets.parse(resp.http_response.body)
end
simulate_response do |resp|
resp.data = Core::XML::Parser.new(XML::ListBuckets.rules).simulate
end
end
# Sets the access policy for a bucket.
# @overload set_bucket_policy(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :policy This can be a String
# or any object that responds to `#to_json`.
# @return [Core::Response]
bucket_method(:set_bucket_policy, :put, 'policy') do
configure_request do |req, options|
require_policy!(options[:policy])
super(req, options)
policy = options[:policy]
policy = policy.to_json unless policy.respond_to?(:to_str)
req.body = policy
end
end
# Gets the access policy for a bucket.
# @overload get_bucket_policy(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_policy, :get, 'policy') do
process_response do |resp|
resp.data[:policy] = resp.http_response.body
end
end
# Deletes the access policy for a bucket.
# @overload delete_bucket_policy(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:delete_bucket_policy, :delete, 'policy')
# @overload set_bucket_versioning(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :state
# @option options [String] :mfa_delete
# @option options [String] :mfa
# @return [Core::Response]
bucket_method(:set_bucket_versioning, :put, 'versioning', :header_options => { :mfa => "x-amz-mfa" }) do
configure_request do |req, options|
state = options[:state].to_s.downcase.capitalize
unless state =~ /^(Enabled|Suspended)$/
raise ArgumentError, "invalid versioning state `#{state}`"
end
# Leave validation of MFA options to S3
mfa_delete = options[:mfa_delete].to_s.downcase.capitalize if options[:mfa_delete]
# Generate XML request for versioning
req.body = Nokogiri::XML::Builder.new do |xml|
xml.VersioningConfiguration('xmlns' => XMLNS) do
xml.Status(state)
xml.MfaDelete(mfa_delete) if mfa_delete
end
end.doc.root.to_xml
super(req, options)
end
end
# Gets the bucket's location constraint.
# @overload get_bucket_location(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_location, :get, 'location') do
process_response do |response|
regex = />(.*)<\/LocationConstraint>/
matches = response.http_response.body.to_s.match(regex)
response.data[:location_constraint] = matches ? matches[1] : nil
end
end
# @overload put_bucket_logging(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [Boolean] :logging_enabled Set to true if turning on
# bucket logging. If not set or false, all of the following options
# will be ignored.
# @option options [String] :target_bucket The name of the bucket in
# which you want Amazon S3 to store server access logs. You can push
# logs to any bucket you own, including the bucket being logged.
# @option options [String] :target_prefix Allows you to specify a prefix
# for the keys that the log files will be stored under. Recommended
# if you will be writing logs from multiple buckets to the same target
# bucket.
# @option options [Array<Hash>] :grants An array of hashes specifying
# permission grantees. For each hash, specify ONLY ONE of :id, :uri,
# or :email_address.
# * `:email_address` - (String) E-mail address of the person being
# granted logging permissions.
# * `:id` - (String) The canonical user ID of the grantee.
# * `:uri` - (String) URI of the grantee group.
# * `:permission` - (String) Logging permissions given to the Grantee
# for the bucket. The bucket owner is automatically granted FULL_CONTROL
# to all logs delivered to the bucket. This optional element enables
# you grant access to others. Valid Values: FULL_CONTROL | READ | WRITE
# @return [Core::Response]
bucket_method(:put_bucket_logging, :put) do
configure_request do |req, options|
req.add_param('logging')
xml = Nokogiri::XML::Builder.new
xml.BucketLoggingStatus('xmlns' => XMLNS) do |xml|
if options[:logging_enabled] == true
xml.LoggingEnabled do
xml.TargetBucket(options[:target_bucket])
xml.TargetPrefix(options[:target_prefix])
unless options[:grants].nil?
xml.TargetGrants do
options[:grants].each do |grant|
xml.Grant do
if !grant[:email_address].nil?
xml.Grantee('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:type' => 'AmazonCustomerByEmail') do
xml.EmailAddress(grant[:email_address])
end
elsif !grant[:uri].nil?
xml.Grantee('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:type' => 'Group') do
xml.URI(grant[:uri])
end
elsif !grant[:id].nil?
xml.Grantee('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:type' => 'CanonicalUser') do
xml.ID(grant[:id])
end
end
xml.Permission(grant[:permission])
end
end
end
end
end
end
end
xml = xml.doc.root.to_xml
req.body = xml
req.headers['content-md5'] = md5(xml)
super(req, options)
end
end
# Gets the bucket's logging status.
# @overload get_bucket_logging(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_logging, :get, 'logging',
XML::GetBucketLogging)
# @overload get_bucket_versioning(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_versioning, :get, 'versioning',
XML::GetBucketVersioning)
# @overload list_object_versions(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [String] :prefix
# @option options [String] :delimiter
# @option options [String] :max_keys
# @option options [String] :key_marker
# @option options [String] :version_id_marker
# @return [Core::Response]
bucket_method(:list_object_versions, :get, 'versions',
XML::ListObjectVersions) do
configure_request do |req, options|
super(req, options)
params = %w(delimiter key_marker max_keys prefix version_id_marker)
params.each do |param|
if options[param.to_sym]
req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
end
end
end
end
# Sets the access control list for a bucket. You must specify an ACL
# via one of the following methods:
#
# * as a canned ACL (via `:acl`)
# * as a list of grants (via the `:grant_*` options)
# * as an access control policy document (via `:access_control_policy`)
#
# @example Using a canned acl
# s3_client.put_bucket_acl(
# :bucket_name => 'bucket-name',
# :acl => 'public-read')
#
# @example Using grants
# s3_client.put_bucket_acl(
# :bucket_name => 'bucket-name',
# :grant_read => 'uri="http://acs.amazonaws.com/groups/global/AllUsers"',
# :grant_full_control => 'emailAddress="xyz@amazon.com", id="8a9...fa7"')
#
# @example Using an access control policy document
# policy_xml = <<-XML
# <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
# <Owner>
# <ID>852b113e7a2f25102679df27bb0ae12b3f85be6BucketOwnerCanonicalUserID</ID>
# <DisplayName>OwnerDisplayName</DisplayName>
# </Owner>
# <AccessControlList>
# <Grant>
# <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
# <ID>BucketOwnerCanonicalUserID</ID>
# <DisplayName>OwnerDisplayName</DisplayName>
# </Grantee>
# <Permission>FULL_CONTROL</Permission>
# </Grant>
# <Grant>
# <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
# <URI xmlns="">http://acs.amazonaws.com/groups/global/AllUsers</URI>
# </Grantee>
# <Permission xmlns="">READ</Permission>
# </Grant>
# </AccessControlList>
# </AccessControlPolicy>
#
# XML
# s3_client.put_bucket_acl(
# :bucket_name => 'bucket-name',
# :access_control_policy => policy_xml)
#
# @overload put_bucket_acl(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [String] :access_control_policy An access control
# policy description as a string of XML. See the S3 API
# documentation for a description.
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @return [Core::Response]
bucket_method(:put_bucket_acl, :put, 'acl', :header_options => {
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
}) do
configure_request do |req, options|
move_access_control_policy(options)
require_acl!(options)
super(req, options)
req.body = options[:access_control_policy] if
options[:access_control_policy]
end
end
alias_method :set_bucket_acl, :put_bucket_acl
# Gets the access control list for a bucket.
# @overload get_bucket_acl(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @return [Core::Response]
bucket_method(:get_bucket_acl, :get, 'acl', XML::GetBucketAcl)
# Sets the access control list for an object. You must specify an ACL
# via one of the following methods:
#
# * as a canned ACL (via `:acl`)
# * as a list of grants (via the `:grant_*` options)
# * as an access control policy document (via `:access_control_policy`)
#
# @example Using a canned acl
# s3_client.put_object_acl(
# :bucket_name => 'bucket-name',
# :key => 'object-key',
# :acl => 'public-read')
#
# @example Using grants
# s3_client.put_bucket_acl(
# :bucket_name => 'bucket-name',
# :key => 'object-key',
# :grant_read => 'uri="http://acs.amazonaws.com/groups/global/AllUsers"',
# :grant_full_control => 'emailAddress="xyz@amazon.com", id="8a9...fa7"')
#
# @example Using an access control policy document
# policy_xml = <<-XML
# <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
# <Owner>
# <ID>852b113e7a2f25102679df27bb0ae12b3f85be6BucketOwnerCanonicalUserID</ID>
# <DisplayName>OwnerDisplayName</DisplayName>
# </Owner>
# <AccessControlList>
# <Grant>
# <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
# <ID>BucketOwnerCanonicalUserID</ID>
# <DisplayName>OwnerDisplayName</DisplayName>
# </Grantee>
# <Permission>FULL_CONTROL</Permission>
# </Grant>
# <Grant>
# <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
# <URI xmlns="">http://acs.amazonaws.com/groups/global/AllUsers</URI>
# </Grantee>
# <Permission xmlns="">READ</Permission>
# </Grant>
# </AccessControlList>
# </AccessControlPolicy>
#
# XML
# s3_client.put_bucket_acl(
# :bucket_name => 'bucket-name',
# :key => 'object-key',
# :access_control_policy => policy_xml)
#
# @overload put_object_acl(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [String] :access_control_policy An access control
# policy description as a string of XML. See the S3 API
# documentation for a description.
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @return [Core::Response]
object_method(:put_object_acl, :put, 'acl', :header_options => {
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
}) do
configure_request do |req, options|
move_access_control_policy(options)
require_acl!(options)
super(req, options)
req.body = options[:access_control_policy] if
options[:access_control_policy]
end
end
alias_method :set_object_acl, :put_object_acl
# Gets the access control list for an object.
# @overload get_object_acl(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @return [Core::Response]
object_method(:get_object_acl, :get, 'acl', XML::GetBucketAcl)
# Puts data into an object, replacing the current contents.
#
# s3_client.put_object({
# :bucket_name => 'bucket-name',
# :key => 'readme.txt',
# :data => 'This is the readme for ...',
# })
#
# @overload put_object(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,String,Pathname,File,IO] :data
# The data to upload. This can be provided as a string,
# a Pathname object, or any object that responds to
# `#read` and `#eof?` (e.g. IO, File, Tempfile, StringIO, etc).
# @option options [Integer] :content_length
# Required if you are using block form to write data or if it is
# not possible to determine the size of `:data`. A best effort
# is made to determine the content length of strings, files,
# tempfiles, io objects, and any object that responds
# to `#length` or `#size`.
# @option options [String] :website_redirect_location If the bucket is
# configured as a website, redirects requests for this object to
# another object in the same bucket or to an external URL.
# @option options [Hash] :metadata
# A hash of metadata to be included with the
# object. These will be sent to S3 as headers prefixed with
# `x-amz-meta`.
# @option options [Symbol] :acl (:private) A canned access
# control policy. Accepted values include:
# * `:private`
# * `:public_read`
# * ...
# @option options [String] :storage_class+ ('STANDARD')
# Controls whether Reduced Redundancy Storage is enabled for
# the object. Valid values are 'STANDARD' and
# 'REDUCED_REDUNDANCY'.
# @option options [Symbol,String] :server_side_encryption (nil) The
# algorithm used to encrypt the object on the server side
# (e.g. :aes256).
# object on the server side, e.g. `:aes256`)
# @option options [String] :cache_control
# Can be used to specify caching behavior.
# @option options [String] :content_disposition
# Specifies presentational information.
# @option options [String] :content_encoding
# Specifies the content encoding.
# @option options [String] :content_md5
# The base64 encoded content md5 of the `:data`.
# @option options [String] :content_type
# Specifies the content type.
# @option options [String] :expires The date and time at which the
# object is no longer cacheable.
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @option options [String] :sse_customer_algorithm Specifies the
# algorithm to use to when encrypting the object (e.g., AES256).
# @option options [String] :sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use in encrypting
# data. This value is used to store the object and then it is
# discarded; Amazon does not store the encryption key. The key must be
# appropriate for use with the algorithm specified in the
# `:sse_customer_algorithm` header.
# @option options [String] :sse_customer_key_md5 Specifies the 128-bit
# MD5 digest of the encryption key according to RFC 1321. Amazon S3
# uses this header for a message integrity check to ensure the
# encryption key was transmitted without error.
# @return [Core::Response]
#
object_method(:put_object, :put, :header_options => {
:website_redirect_location => 'x-amz-website-redirect-location',
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
:content_md5 => 'Content-MD5',
:cache_control => 'Cache-Control',
:content_disposition => 'Content-Disposition',
:content_encoding => 'Content-Encoding',
:content_type => 'Content-Type',
:expires => 'Expires',
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
}) do
configure_request do |request, options|
options = compute_write_options(options)
set_body_stream_and_content_length(request, options)
set_metadata(request, options)
set_storage_class(request, options)
set_server_side_encryption(request, options)
super(request, options)
end
process_response do |resp|
extract_object_headers(resp)
end
simulate_response do |response|
response.data[:etag] = 'abc123'
response.data[:version_id] = nil
end
end
# Gets the data for a key.
# @overload get_object(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [String] :request_payer If specified, the request
# will contain the specified String value in the x-amz-request-payer
# header. This is required for Requester Pays enabled buckets.
# @option options [Time] :if_modified_since If specified, the
# response will contain an additional `:modified` value that
# returns true if the object was modified after the given
# time. If `:modified` is false, then the response
# `:data` value will be `nil`.
# @option options [Time] :if_unmodified_since If specified, the
# response will contain an additional `:unmodified` value
# that is true if the object was not modified after the
# given time. If `:unmodified` returns false, the `:data`
# value will be `nil`.
# @option options [String] :if_match If specified, the response
# will contain an additional `:matches` value that is true
# if the object ETag matches the value for this option. If
# `:matches` is false, the `:data` value of the
# response will be `nil`.
# @option options [String] :if_none_match If specified, the
# response will contain an additional `:matches` value that
# is true if and only if the object ETag matches the value for
# this option. If `:matches` is true, the `:data` value
# of the response will be `nil`.
# @option options [String] :sse_customer_algorithm Specifies the
# algorithm to use to when encrypting the object (e.g., AES256).
# @option options [String] :sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use in encrypting
# data. This value is used to store the object and then it is
# discarded; Amazon does not store the encryption key. The key must be
# appropriate for use with the algorithm specified in the
# `:sse_customer_algorithm` header.
# @option options [String] :sse_customer_key_md5 Specifies the 128-bit
# MD5 digest of the encryption key according to RFC 1321. Amazon S3
# uses this header for a message integrity check to ensure the
# encryption key was transmitted without error.
# @option options [Range<Integer>] :range A byte range of data to request.
# @return [Core::Response]
#
object_method(:get_object, :get,
:header_options => {
:request_payer => "x-amz-request-payer",
:if_modified_since => "If-Modified-Since",
:if_unmodified_since => "If-Unmodified-Since",
:if_match => "If-Match",
:if_none_match => "If-None-Match",
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
}) do
configure_request do |req, options|
super(req, options)
if options[:version_id]
req.add_param('versionId', options[:version_id])
end
["If-Modified-Since",
"If-Unmodified-Since"].each do |date_header|
case value = req.headers[date_header]
when DateTime
req.headers[date_header] = Time.parse(value.to_s).rfc2822
when Time
req.headers[date_header] = value.rfc2822
end
end
if options[:range]
range = options[:range]
if range.is_a?(Range)
offset = range.exclude_end? ? -1 : 0
range = "bytes=#{range.first}-#{range.last + offset}"
end
req.headers['Range'] = range
end
end
process_response do |resp|
extract_object_headers(resp)
resp.data[:data] = resp.http_response.body
end
end
# Gets the torrent for a key.
# @overload get_object_torrent(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @return [Core::Response]
#
object_method(:get_object_torrent, :get, 'torrent') do
process_response do |resp|
extract_object_headers(resp)
resp.data[:data] = resp.http_response.body
end
end
# @overload head_object(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [String] :version_id
# @option options [Time] :if_modified_since If specified, the
# response will contain an additional `:modified` value that
# returns true if the object was modified after the given
# time. If `:modified` is false, then the response
# `:data` value will be `nil`.
# @option options [Time] :if_unmodified_since If specified, the
# response will contain an additional `:unmodified` value
# that is true if the object was not modified after the
# given time. If `:unmodified` returns false, the `:data`
# value will be `nil`.
# @option options [String] :if_match If specified, the response
# will contain an additional `:matches` value that is true
# if the object ETag matches the value for this option. If
# `:matches` is false, the `:data` value of the
# response will be `nil`.
# @option options [String] :if_none_match If specified, the
# response will contain an additional `:matches` value that
# is true if and only if the object ETag matches the value for
# this option. If `:matches` is true, the `:data` value
# of the response will be `nil`.
# @option options [String] :sse_customer_algorithm Specifies the
# algorithm to use to when encrypting the object (e.g., AES256).
# @option options [String] :sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use in encrypting
# data. This value is used to store the object and then it is
# discarded; Amazon does not store the encryption key. The key must be
# appropriate for use with the algorithm specified in the
# `:sse_customer_algorithm` header.
# @option options [String] :sse_customer_key_md5 Specifies the 128-bit
# MD5 digest of the encryption key according to RFC 1321. Amazon S3
# uses this header for a message integrity check to ensure the
# encryption key was transmitted without error.
# @option options [Range<Integer>] :range A byte range of data to request.
# @return [Core::Response]
object_method(:head_object, :head,
:header_options => {
:if_modified_since => "If-Modified-Since",
:if_unmodified_since => "If-Unmodified-Since",
:if_match => "If-Match",
:if_none_match => "If-None-Match",
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
}) do
configure_request do |req, options|
super(req, options)
if options[:version_id]
req.add_param('versionId', options[:version_id])
end
["If-Modified-Since",
"If-Unmodified-Since"].each do |date_header|
case value = req.headers[date_header]
when DateTime
req.headers[date_header] = Time.parse(value.to_s).rfc2822
when Time
req.headers[date_header] = value.rfc2822
end
end
if options[:range]
range = options[:range]
if range.is_a?(Range)
offset = range.exclude_end? ? -1 : 0
range = "bytes=#{range.first}-#{range.last + offset}"
end
req.headers['Range'] = range
end
end
process_response do |resp|
extract_object_headers(resp)
end
end
# @overload delete_object(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [String] :version_id
# @option options [String] :mfa
# @return [Core::Response]
object_method(:delete_object, :delete, :header_options => { :mfa => "x-amz-mfa" }) do
configure_request do |req, options|
super(req, options)
if options[:version_id]
req.add_param('versionId', options[:version_id])
end
end
process_response do |resp|
resp.data[:version_id] = resp.http_response.header('x-amz-version-id')
end
end
# @overload restore_object(options = {})
# Restores a temporary copy of an archived object.
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,Integer] :days the number of days to keep
# the restored object.
# @return [Core::Response]
# @since 1.7.2
object_method(:restore_object, :post, 'restore',
:header_options => { :content_md5 => 'Content-MD5' }) do
configure_request do |req, options|
super(req, options)
validate!(:days, options[:days]) do
"must be greater or equal to 1" if options[:days].to_i <= 0
end
xml = Nokogiri::XML::Builder.new do |xml|
xml.RestoreRequest('xmlns' => XMLNS) do
xml.Days(options[:days].to_i) if options[:days]
end
end.doc.root.to_xml
req.body = xml
req.headers['content-type'] = 'application/xml'
req.headers['content-md5'] = md5(xml)
end
end
# @overload list_objects(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [String] :delimiter
# @option options [String] :marker
# @option options [String] :max_keys
# @option options [String] :prefix
# @return [Core::Response]
bucket_method(:list_objects, :get, XML::ListObjects) do
configure_request do |req, options|
super(req, options)
params = %w(delimiter marker max_keys prefix)
params.each do |param|
if options[param.to_sym]
req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
end
end
end
end
alias_method :get_bucket, :list_objects
# @overload initiate_multipart_upload(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [String] :website_redirect_location If the bucket is
# configured as a website, redirects requests for this object to
# another object in the same bucket or to an external URL.
# @option options [Hash] :metadata
# @option options [Symbol] :acl
# @option options [String] :cache_control
# @option options [String] :content_disposition
# @option options [String] :content_encoding
# @option options [String] :content_type
# @option options [String] :storage_class+ ('STANDARD')
# Controls whether Reduced Redundancy Storage is enabled for
# the object. Valid values are 'STANDARD' and
# 'REDUCED_REDUNDANCY'.
# @option options [Symbol,String] :server_side_encryption (nil) The
# algorithm used to encrypt the object on the server side
# (e.g. :aes256).
# @option options [String] :expires The date and time at which the
# object is no longer cacheable.
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @option options [String] :sse_customer_algorithm Specifies the
# algorithm to use to when encrypting the object (e.g., AES256).
# @option options [String] :sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use in encrypting
# data. This value is used to store the object and then it is
# discarded; Amazon does not store the encryption key. The key must be
# appropriate for use with the algorithm specified in the
# `:sse_customer_algorithm` header.
# @option options [String] :sse_customer_key_md5 Specifies the 128-bit
# MD5 digest of the encryption key according to RFC 1321. Amazon S3
# uses this header for a message integrity check to ensure the
# encryption key was transmitted without error.
# @return [Core::Response]
object_method(:initiate_multipart_upload, :post, 'uploads',
XML::InitiateMultipartUpload,
:header_options => {
:website_redirect_location => 'x-amz-website-redirect-location',
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
:cache_control => 'Cache-Control',
:content_disposition => 'Content-Disposition',
:content_encoding => 'Content-Encoding',
:content_type => 'Content-Type',
:expires => 'Expires',
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
}) do
configure_request do |req, options|
set_metadata(req, options)
set_storage_class(req, options)
set_server_side_encryption(req, options)
super(req, options)
end
process_response do |resp|
extract_object_headers(resp)
end
end
# @overload list_multipart_uploads(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [String] :delimiter
# @option options [String] :key_marker
# @option options [String] :max_keys
# @option options [String] :upload_id_marker
# @option options [String] :max_uploads
# @option options [String] :prefix
# @return [Core::Response]
bucket_method(:list_multipart_uploads,
:get, 'uploads',
XML::ListMultipartUploads) do
configure_request do |req, options|
super(req, options)
params = %w(delimiter key_marker max_keys) +
%w(upload_id_marker max_uploads prefix)
params.each do |param|
if options[param.to_sym]
req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
end
end
end
end
# @overload delete_objects(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,Array<Hash>] :objects Each entry should be
# a hash with the following keys:
# * `:key` - *required*
# * `:version_id`
# @option options [Boolean] :quiet (true)
# @option options [String] :mfa
# @return [Core::Response]
bucket_method(:delete_objects, :post, 'delete', XML::DeleteObjects,
:header_options => { :mfa => "x-amz-mfa" }
) do
configure_request do |req, options|
super(req, options)
req.body = Nokogiri::XML::Builder.new do |xml|
xml.Delete do
xml.Quiet(options.key?(:quiet) ? options[:quiet] : true)
(options[:objects] || options[:keys]).each do |obj|
xml.Object do
xml.Key(obj[:key])
xml.VersionId(obj[:version_id]) if obj[:version_id]
end
end
end
end.doc.root.to_xml
req.headers['content-md5'] = md5(req.body)
end
end
# @overload upload_part(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,String] :upload_id
# @option options [required,Integer] :part_number
# @option options [required,String,Pathname,File,IO] :data
# The data to upload. This can be provided as a string,
# a Pathname object, or any object that responds to
# `#read` and `#eof?` (e.g. IO, File, Tempfile, StringIO, etc).
# @return [Core::Response]
object_method(:upload_part, :put,
:header_options => {
:content_md5 => 'Content-MD5',
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
}) do
configure_request do |request, options|
options = compute_write_options(options)
set_body_stream_and_content_length(request, options)
require_upload_id!(options[:upload_id])
request.add_param('uploadId', options[:upload_id])
require_part_number!(options[:part_number])
request.add_param('partNumber', options[:part_number])
super(request, options)
end
process_response do |resp|
extract_object_headers(resp)
end
simulate_response do |response|
response.data[:etag] = 'abc123'
end
end
# @overload complete_multipart_upload(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,String] :upload_id
# @option options [required,Array<Hash>] :parts An array of hashes
# with the following keys:
# * `:part_number` [Integer] - *required*
# * `:etag` [String] - *required*
# @return [Core::Response]
object_method(:complete_multipart_upload, :post,
XML::CompleteMultipartUpload) do
configure_request do |req, options|
require_upload_id!(options[:upload_id])
validate_parts!(options[:parts])
super(req, options)
req.add_param('uploadId', options[:upload_id])
req.body = Nokogiri::XML::Builder.new do |xml|
xml.CompleteMultipartUpload do
options[:parts].each do |part|
xml.Part do
xml.PartNumber(part[:part_number])
xml.ETag(part[:etag])
end
end
end
end.doc.root.to_xml
end
process_response do |resp|
extract_object_headers(resp)
end
simulate_response do |response|
response.data = {}
end
end
# @overload abort_multipart_upload(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,String] :upload_id
# @return [Core::Response]
object_method(:abort_multipart_upload, :delete) do
configure_request do |req, options|
require_upload_id!(options[:upload_id])
super(req, options)
req.add_param('uploadId', options[:upload_id])
end
end
# @overload list_parts(options = {})
# @param [Hash] options
# @option options [required,String] :bucket_name
# @option options [required,String] :key
# @option options [required,String] :upload_id
# @option options [Integer] :max_parts
# @option options [Integer] :part_number_marker
# @return [Core::Response]
object_method(:list_parts, :get, XML::ListParts) do
configure_request do |req, options|
require_upload_id!(options[:upload_id])
super(req, options)
req.add_param('uploadId', options[:upload_id])
req.add_param('max-parts', options[:max_parts])
req.add_param('part-number-marker', options[:part_number_marker])
end
end
# Copies an object from one key to another.
# @overload copy_object(options = {})
# @param [Hash] options
# @option options [required, String] :bucket_name Name of the bucket
# to copy a object into.
# @option options [required, String] :key Where (object key) in the
# bucket the object should be copied to.
# @option options [String] :website_redirect_location If the bucket is
# configured as a website, redirects requests for this object to
# another object in the same bucket or to an external URL.
# @option options [required, String] :copy_source The source
# bucket name and key, joined by a forward slash ('/').
# This string must be URL-encoded. Additionally, you must
# have read access to the source object.
# @option options [String] :acl A canned ACL (e.g. 'private',
# 'public-read', etc). See the S3 API documentation for
# a complete list of valid values.
# @option options [Symbol,String] :server_side_encryption (nil) The
# algorithm used to encrypt the object on the server side
# (e.g. :aes256).
# @option options [String] :storage_class+ ('STANDARD')
# Controls whether Reduced Redundancy Storage is enabled for
# the object. Valid values are 'STANDARD' and
# 'REDUCED_REDUNDANCY'.
# @option options [String] :metadata_directive ('COPY') Specify 'COPY' or
# 'REPLACE'.
# @option options [String] :content_type
# @option options [String] :content_encoding
# @option options [String] :content_disposition
# @option options [String] :cache_control
# @option options [String] :expires The date and time at which the
# object is no longer cacheable.
# @option options [String] :grant_read
# @option options [String] :grant_write
# @option options [String] :grant_read_acp
# @option options [String] :grant_write_acp
# @option options [String] :grant_full_control
# @option options [String] :sse_customer_algorithm Specifies the
# algorithm to use to when encrypting the object (e.g., AES256).
# @option options [String] :sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use in encrypting
# data. This value is used to store the object and then it is
# discarded; Amazon does not store the encryption key. The key must be
# appropriate for use with the algorithm specified in the
# `:sse_customer_algorithm` header.
# @option options [String] :sse_customer_key_md5 Specifies the 128-bit
# MD5 digest of the encryption key according to RFC 1321. Amazon S3
# uses this header for a message integrity check to ensure the
# encryption key was transmitted without error.
# @option options [String] :copy_source_sse_customer_algorithm Specifies
# the algorithm to use when decrypting the source object (e.g.,
# AES256).
# @option options [String] :copy_source_sse_customer_key Specifies the
# customer-provided encryption key for Amazon S3 to use to decrypt the
# source object. The encryption key provided in this header must be
# one that was used when the source object was created.
# @option options [String] :copy_source_sse_customer_key_md5 Specifies
# the 128-bit MD5 digest of the encryption key according to RFC 1321.
# Amazon S3 uses this header for a message integrity check to ensure
# the encryption key was transmitted without error.
# @return [Core::Response]
object_method(:copy_object, :put, :header_options => {
:website_redirect_location => 'x-amz-website-redirect-location',
:acl => 'x-amz-acl',
:grant_read => 'x-amz-grant-read',
:grant_write => 'x-amz-grant-write',
:grant_read_acp => 'x-amz-grant-read-acp',
:grant_write_acp => 'x-amz-grant-write-acp',
:grant_full_control => 'x-amz-grant-full-control',
:copy_source => 'x-amz-copy-source',
:cache_control => 'Cache-Control',
:metadata_directive => 'x-amz-metadata-directive',
:content_type => 'Content-Type',
:content_encoding => 'Content-Encoding',
:content_disposition => 'Content-Disposition',
:expires => 'Expires',
:sse_customer_algorithm => 'x-amz-server-side-encryption-customer-algorithm',
:sse_customer_key => 'x-amz-server-side-encryption-customer-key',
:sse_customer_key_md5 => 'x-amz-server-side-encryption-customer-key-MD5',
:copy_source_sse_customer_algorithm => 'x-amz-copy-source-server-side-encryption-customer-algorithm',
:copy_source_sse_customer_key => 'x-amz-copy-source-server-side-encryption-customer-key',
:copy_source_sse_customer_key_md5 => 'x-amz-copy-source-server-side-encryption-customer-key-MD5',
}) do
configure_request do |req, options|
validate!(:copy_source, options[:copy_source]) do
"may not be blank" if options[:copy_source].to_s.empty?
end
options = options.merge(:copy_source => escape_path(options[:copy_source]))
super(req, options)
set_metadata(req, options)
set_storage_class(req, options)
set_server_side_encryption(req, options)
if options[:version_id]
req.headers['x-amz-copy-source'] += "?versionId=#{options[:version_id]}"
end
end
process_response do |resp|
extract_object_headers(resp)
end
end
object_method(:copy_part, :put, XML::CopyPart, :header_options => {
:copy_source => 'x-amz-copy-source',
:copy_source_range => 'x-amz-copy-source-range',
}) do
configure_request do |request, options|
validate!(:copy_source, options[:copy_source]) do
"may not be blank" if options[:copy_source].to_s.empty?
end
validate!(:copy_source_range, options[:copy_source_range]) do
"must start with bytes=" if options[:copy_source_range] && !options[:copy_source_range].start_with?("bytes=")
end
options = options.merge(:copy_source => escape_path(options[:copy_source]))
require_upload_id!(options[:upload_id])
request.add_param('uploadId', options[:upload_id])
require_part_number!(options[:part_number])
request.add_param('partNumber', options[:part_number])
super(request, options)
if options[:version_id]
req.headers['x-amz-copy-source'] += "?versionId=#{options[:version_id]}"
end
end
end
end
end
end
|