|
1 | 1 | /* |
2 | | - * Minio Java Library for Amazon S3 Compatible Cloud Storage, (C) 2015 Minio, Inc. |
| 2 | + * Minio Java Library for Amazon S3 Compatible Cloud Storage, |
| 3 | + * (C) 2015, 2016, 2017 Minio, Inc. |
3 | 4 | * |
4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 6 | * you may not use this file except in compliance with the License. |
|
38 | 39 | import io.minio.http.Scheme; |
39 | 40 | import io.minio.messages.Bucket; |
40 | 41 | import io.minio.messages.CompleteMultipartUpload; |
| 42 | +import io.minio.messages.CopyObjectResult; |
41 | 43 | import io.minio.messages.CreateBucketConfiguration; |
42 | 44 | import io.minio.messages.ErrorResponse; |
43 | 45 | import io.minio.messages.InitiateMultipartUploadResult; |
@@ -1076,7 +1078,6 @@ private HttpResponse executePut(String bucketName, String objectName, Map<String |
1076 | 1078 | HttpResponse response = execute(Method.PUT, region, bucketName, objectName, |
1077 | 1079 | headerMap, queryParamMap, |
1078 | 1080 | data, length); |
1079 | | - response.body().close(); |
1080 | 1081 | return response; |
1081 | 1082 | } |
1082 | 1083 |
|
@@ -1397,6 +1398,186 @@ public void getObject(String bucketName, String objectName, String fileName) |
1397 | 1398 | } |
1398 | 1399 | } |
1399 | 1400 |
|
| 1401 | + /** |
| 1402 | + * Copy a source object into a new destination object with same object name. |
| 1403 | + * |
| 1404 | + * </p> |
| 1405 | + * <b>Example:</b><br> |
| 1406 | + * |
| 1407 | + * <pre> |
| 1408 | + * {@code minioClient.copyObject("my-bucketname", "my-objectname", "my-destbucketname");} |
| 1409 | + * </pre> |
| 1410 | + * |
| 1411 | + * @param bucketName |
| 1412 | + * Bucket name where the object to be copied exists. |
| 1413 | + * @param objectName |
| 1414 | + * Object name source to be copied. |
| 1415 | + * @param destBucketName |
| 1416 | + * Bucket name where the object will be copied to. |
| 1417 | + * |
| 1418 | + * @throws InvalidBucketNameException |
| 1419 | + * upon an invalid bucket name |
| 1420 | + * @throws NoSuchAlgorithmException |
| 1421 | + * upon requested algorithm was not found during signature calculation |
| 1422 | + * @throws InvalidKeyException |
| 1423 | + * upon an invalid access key or secret key |
| 1424 | + */ |
| 1425 | + public void copyObject(String bucketName, String objectName, String destBucketName) |
| 1426 | + throws InvalidKeyException, InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, |
| 1427 | + NoResponseException, ErrorResponseException, InternalException, IOException, XmlPullParserException, |
| 1428 | + InvalidArgumentException { |
| 1429 | + |
| 1430 | + copyObject(bucketName, objectName, destBucketName, null, null); |
| 1431 | + } |
| 1432 | + |
| 1433 | + /** |
| 1434 | + * Copy a source object into a new destination object. |
| 1435 | + * |
| 1436 | + * </p> |
| 1437 | + * <b>Example:</b><br> |
| 1438 | + * |
| 1439 | + * <pre> |
| 1440 | + * {@code minioClient.copyObject("my-bucketname", "my-objectname", "my-destbucketname", "my-destobjname");} |
| 1441 | + * </pre> |
| 1442 | + * |
| 1443 | + * @param bucketName |
| 1444 | + * Bucket name where the object to be copied exists. |
| 1445 | + * @param objectName |
| 1446 | + * Object name source to be copied. |
| 1447 | + * @param destBucketName |
| 1448 | + * Bucket name where the object will be copied to. |
| 1449 | + * @param destObjectName |
| 1450 | + * Object name to be created, if not provided uses source object name |
| 1451 | + * as destination object name. |
| 1452 | + * |
| 1453 | + * @throws InvalidBucketNameException |
| 1454 | + * upon an invalid bucket name |
| 1455 | + * @throws NoSuchAlgorithmException |
| 1456 | + * upon requested algorithm was not found during signature calculation |
| 1457 | + * @throws InvalidKeyException |
| 1458 | + * upon an invalid access key or secret key |
| 1459 | + */ |
| 1460 | + public void copyObject(String bucketName, String objectName, String destBucketName, String destObjectName) |
| 1461 | + throws InvalidKeyException, InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, |
| 1462 | + NoResponseException, ErrorResponseException, InternalException, IOException, XmlPullParserException, |
| 1463 | + InvalidArgumentException { |
| 1464 | + |
| 1465 | + copyObject(bucketName, objectName, destBucketName, destObjectName, null); |
| 1466 | + } |
| 1467 | + |
| 1468 | + /** |
| 1469 | + * Copy a source object into a new object with the provided name in the provided bucket. |
| 1470 | + * optionally can take a key value CopyConditions as well for conditionally attempting |
| 1471 | + * copyObject. |
| 1472 | + * |
| 1473 | + * </p> |
| 1474 | + * <b>Example:</b><br> |
| 1475 | + * |
| 1476 | + * <pre> |
| 1477 | + * {@code minioClient.copyObject("my-bucketname", "my-objectname", "my-destbucketname", |
| 1478 | + * copyConditions);} |
| 1479 | + * </pre> |
| 1480 | + * |
| 1481 | + * @param bucketName |
| 1482 | + * Bucket name where the object to be copied exists. |
| 1483 | + * @param objectName |
| 1484 | + * Object name source to be copied. |
| 1485 | + * @param destBucketName |
| 1486 | + * Bucket name where the object will be copied to. |
| 1487 | + * @param copyConditions |
| 1488 | + * CopyConditions object with collection of supported CopyObject conditions. |
| 1489 | + * |
| 1490 | + * @throws InvalidBucketNameException |
| 1491 | + * upon an invalid bucket name |
| 1492 | + * @throws NoSuchAlgorithmException |
| 1493 | + * upon requested algorithm was not found during signature calculation |
| 1494 | + * @throws InvalidKeyException |
| 1495 | + * upon an invalid access key or secret key |
| 1496 | + */ |
| 1497 | + public void copyObject(String bucketName, String objectName, String destBucketName, |
| 1498 | + CopyConditions copyConditions) |
| 1499 | + throws InvalidKeyException, InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, |
| 1500 | + NoResponseException, ErrorResponseException, InternalException, IOException, XmlPullParserException, |
| 1501 | + InvalidArgumentException { |
| 1502 | + |
| 1503 | + copyObject(bucketName, objectName, destBucketName, null, copyConditions); |
| 1504 | + } |
| 1505 | + |
| 1506 | + /** |
| 1507 | + * Copy a source object into a new object with the provided name in the provided bucket. |
| 1508 | + * optionally can take a key value CopyConditions as well for conditionally attempting |
| 1509 | + * copyObject. |
| 1510 | + * |
| 1511 | + * </p> |
| 1512 | + * <b>Example:</b><br> |
| 1513 | + * |
| 1514 | + * <pre> |
| 1515 | + * {@code minioClient.copyObject("my-bucketname", "my-objectname", "my-destbucketname", |
| 1516 | + * "my-destobjname", copyConditions);} |
| 1517 | + * </pre> |
| 1518 | + * |
| 1519 | + * @param bucketName |
| 1520 | + * Bucket name where the object to be copied exists. |
| 1521 | + * @param objectName |
| 1522 | + * Object name source to be copied. |
| 1523 | + * @param destBucketName |
| 1524 | + * Bucket name where the object will be copied to. |
| 1525 | + * @param destObjectName |
| 1526 | + * Object name to be created, if not provided uses source object name |
| 1527 | + * as destination object name. |
| 1528 | + * @param copyConditions |
| 1529 | + * CopyConditions object with collection of supported CopyObject conditions. |
| 1530 | + * |
| 1531 | + * @throws InvalidBucketNameException |
| 1532 | + * upon an invalid bucket name, invalid object name. |
| 1533 | + * @throws NoSuchAlgorithmException |
| 1534 | + * upon requested algorithm was not found during signature calculation |
| 1535 | + * @throws InvalidKeyException |
| 1536 | + * upon an invalid access key or secret key |
| 1537 | + */ |
| 1538 | + public void copyObject(String bucketName, String objectName, String destBucketName, |
| 1539 | + String destObjectName, CopyConditions copyConditions) |
| 1540 | + throws InvalidKeyException, InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, |
| 1541 | + NoResponseException, ErrorResponseException, InternalException, IOException, XmlPullParserException, |
| 1542 | + InvalidArgumentException { |
| 1543 | + |
| 1544 | + if (bucketName == null) { |
| 1545 | + throw new InvalidArgumentException("Source bucket name cannot be empty"); |
| 1546 | + } |
| 1547 | + if (objectName == null) { |
| 1548 | + throw new InvalidArgumentException("Source object name cannot be empty"); |
| 1549 | + } |
| 1550 | + if (destBucketName == null) { |
| 1551 | + throw new InvalidArgumentException("Destination bucket name cannot be empty"); |
| 1552 | + } |
| 1553 | + |
| 1554 | + // Escape source object path. |
| 1555 | + String sourceObjectPath = S3Escaper.encode(Paths.get(bucketName, objectName).toString()); |
| 1556 | + |
| 1557 | + // Destination object name is optional, if empty default to source object name. |
| 1558 | + if (destObjectName == null) { |
| 1559 | + destObjectName = objectName; |
| 1560 | + } |
| 1561 | + |
| 1562 | + Map<String, String> headerMap = new HashMap<>(); |
| 1563 | + |
| 1564 | + // Set the object source |
| 1565 | + headerMap.put("x-amz-copy-source", sourceObjectPath); |
| 1566 | + |
| 1567 | + // If no conditions available, skip addition else add the conditions to the header |
| 1568 | + if (copyConditions != null) { |
| 1569 | + headerMap.putAll(copyConditions.getConditions()); |
| 1570 | + } |
| 1571 | + |
| 1572 | + |
| 1573 | + HttpResponse response = executePut(destBucketName, destObjectName, headerMap, |
| 1574 | + null, null, "", 0); |
| 1575 | + |
| 1576 | + // For now ignore the copyObjectResult, just read and parse it. |
| 1577 | + CopyObjectResult result = new CopyObjectResult(); |
| 1578 | + result.parseXml(response.body().charStream()); |
| 1579 | + response.body().close(); |
| 1580 | + } |
1400 | 1581 |
|
1401 | 1582 | /** |
1402 | 1583 | * Returns an presigned URL to download the object in the bucket with given expiry time with custom request params. |
@@ -1946,7 +2127,8 @@ public void makeBucket(String bucketName, String region) |
1946 | 2127 | configString = config.toString(); |
1947 | 2128 | } |
1948 | 2129 |
|
1949 | | - executePut(bucketName, null, null, null, US_EAST_1, configString, 0); |
| 2130 | + HttpResponse response = executePut(bucketName, null, null, null, US_EAST_1, configString, 0); |
| 2131 | + response.body().close(); |
1950 | 2132 | } |
1951 | 2133 |
|
1952 | 2134 |
|
@@ -2164,6 +2346,7 @@ private String putObject(String bucketName, String objectName, int length, |
2164 | 2346 | } |
2165 | 2347 |
|
2166 | 2348 | HttpResponse response = executePut(bucketName, objectName, headerMap, queryParamMap, data, length); |
| 2349 | + response.body().close(); |
2167 | 2350 | return response.header().etag(); |
2168 | 2351 | } |
2169 | 2352 |
|
@@ -2307,7 +2490,8 @@ private void setBucketPolicy(String bucketName, BucketPolicy policy) |
2307 | 2490 |
|
2308 | 2491 | String policyJson = policy.getJson(); |
2309 | 2492 |
|
2310 | | - executePut(bucketName, null, headerMap, queryParamMap, policyJson, policyJson.length()); |
| 2493 | + HttpResponse response = executePut(bucketName, null, headerMap, queryParamMap, policyJson, policyJson.length()); |
| 2494 | + response.body().close(); |
2311 | 2495 | } |
2312 | 2496 |
|
2313 | 2497 |
|
|
0 commit comments